ES6 Class Class: www.runoob.com/w3cnote/es6…

The symbol data type: www.runoob.com/w3cnote/es6…

class

Define the attention points of the class

ES6 class:

  • All methods of a class are defined on the prototype property of the class
  • When you define a method of a class, you don’t need to add the keyword function before it
  • Methods do not need to be separated by commas
  • Class is used in ES6 exactly like the constructor in ES5
        class Person {
            constructor(name, sex) {
                this.name = name;
                this.sex = sex;
            }
 
            get ps() {
                return 'Using the PS method';
            }
 
            set ps(value) {
                console.log('passing in parameters${value}`);
            }
 
            doing(what) {
                return I was `The ${this.name}, I'm${what}`; }};let xiaoming = new Person('Ming'.24);   // The object must be instantiated through the new method
        console.log(xiaoming.doing('drive'));
Copy the code

Assign adds a class method

Since the methods of the class are defined on the Prototype object, new methods of the class can be added to the prototype object. The object. assign method can add multiple methods to a class at once. Note: When adding multiple methods to the method format of object. assign, separate each method with a comma.

        Object.assign(
            Person.prototype, {
            say() {
                return I call `The ${this.name}This year,The ${this.sex}I'm old, but I'm still single; }});console.log(xiaoming.say());
Copy the code

Instance objects

  • The constructor method is the default method of the class and is automatically called when an object instance is generated with the new command. A class must have a constructor method. If it is not explicitly defined, an empty constructor method will be added by default. The constructor method returns an instance object (i.e., this) by default, and it may be specified to return another object
  • Class must be called with new, otherwise an error will be reported
  • As in ES5, you can use the get and set keywords inside a “class” to set a value function and a value function for a property, blocking access to that property
  • Like functions, classes can be defined in the form of expressions. Classes defined by expressions are created using the name of the expression argument, not the name of the class. In this example, car and CAR
        let car = class Car {
            constructor() {
                this;
            }
 
            self() {
                console.log('****** prints this object ******');
                console.log(this);
                console.log('****** prints this object ******');
            }
 
            manufacturer(country) {
                return This is a `${country}A car made in China; }};let car1 = new car();
        // let car2 = new Car(); // Error: Uncaught ReferenceError: Car is not defined
        // let car3 = car(); // Error: Uncaught TypeError: Class constructor Person cannot be invoked without 'new'
        car1.self();
        console.log(car1.manufacturer('China'));
Copy the code

Static methods, private methods, and private properties

Static methods: The class is the prototype of the instance. All methods defined in the class are inherited by the instance. If you put the static keyword in front of a method, it means that the method will not be inherited by the instance, but will be called directly from the class. This is called a static method.

  • If the static method contains the this keyword, this refers to the class, not the instance
  • Static methods can have the same name as non-static methods
  • A static method of a parent class that can be inherited by a child class
  • Static methods can also be called from super objects

Private methods and properties: Methods and properties that can only be accessed inside a class, not outside

  • You can also add the static keyword before a private property or method to indicate that it is a static private property or method
  • Private properties cannot be written in Constructor
  • Private methods and private properties can be denoted by placing a # before the property name or method name
  • Private methods and private properties can only be called inside the class; external calls will report an error
        class Dog {
            #color = 'yellow';
            constructor(owner, name,color) {
                this.owner = owner;
                this.name = name;
            }
 
            static self() {   // Static methods
                console.log(this);    // If the static method contains the this keyword, this refers to the class, not the instance
                console.log('* * * * * * * * * * * * * * * * *');
                console.log(this.name);
                console.log();
            }
 
            #eat(){
                return `The ${this.name}Do not eat fireworks;
            }
 
            wangwang(){
                console.log(`The ${this.owner}theThe ${this.name}isThe ${this.#color}`);
                console.log(this.#eat()); }}let ahuang = new Dog('Yang Jian'.Wheezy Dog);
        // ahuang.srlf(); Error: Uncaught TypeError: ahuang. Eat is not a function
 
        // ahuang.#eat();
        // console.log(ahuang.#color); Uncaught SyntaxError: Private field '#color' must be declared in an enclosing class
       
        Dog.self();     // Classes can access their own static methods
        ahuang.wangwang();   Private properties and private methods can only be used internally
Copy the code

inheritance

        class Human{
            constructor(name,age){
                this.name = name;
                this.age =age;
                this.head = 1;
                this.arm = 2;
                this.leg = 2;
            }
 
            food(){
                console.log('eat'); }}class Disabled extends Human{
            constructor(){
                super();
            }
        }
 
        // let disability = new Disabled();
        let disability = new Disabled('Disabled'.40);
        disability.food();
Copy the code

A decorator other methods, such as: www.runoob.com/w3cnote/es6…