ES6 Class (Class) (ix)

A, Class Class
1, the constructor

    constructor(){}

Description:
A, if no constructor redefine the parameters of the class will be provided by default with no arguments for our implicit constructor
B, it will automatically call the constructor of the class in the instance creating class

2, examples of the attribute     [ examples of properties defined class constructor class (class instance attributes) ]
eg:

    constructor(name,age){
        this.name = name;
        this.age = age;
    }

Description:
A, instance variables defined in the constructor class
instance b, instance variables only class can call, the class can not be called

3, static properties     [ directly on the class definition property is a static property (kind attribute) ]
forms two definitions:
a, using the previous definitions in the class static keyword example of a method

    static attrName = "static properties";

b, direct use of the class name attribute names defined outside the class definition static properties

    className.attrName = "static properties";


4, an example method     [ methods defined in the class of an instance method (Method class instance) ]

    sayName(){}


5. [static methods defined in the class to modify the static keyword method (class method) ]

    static say(){}

class Example:

    {the Person class
         // constructor 
        constructor (name, Age) {
             the this .name = name;
             the this .age = Age; 
        } 
        // static properties 
        static staticAttr1 = "Static Property 1" ;
         // instance method 
        sayName () {}
         / / static method 
        static say () {} 
    } 
    // static properties 
    Person.staticAttr2 = "static property 2" ;
     // Create instance of the Person class objects 
    the let P = new new Person ( 'NZC', 18 is );
     // instance method invocation and output 
    console.log (p.name, p.age);    // NZC 18 is 
    // static method call and outputs 
    the console.log (Person.staticAttr1, Person.staticAttr2);     // Static Property 1 Static Property 2 
    // call instance method sayName () of 
    p.sayName ();
     // Static method say () call 
    Person.say ();
Examples of the complete class definition


Second, the class inheritance
description:
1, may be used to achieve the extends keyword inheritance, and combinations thereof js native nature of inheritance as
2, two inheritance chain :
A, the __proto__ properties subclasses, inheritance represents a constructor, always points to the parent class
b, __proto__ properties subclass prototype property, expressed inherited methods, always points parent class prototype property

super keyword
as a function of :
a, super () method inherited from other classes, itself constructor using super () call to a procedure similar to the configuration of the parent class
b, parameter super () method with the need to call the parent class constructor like, super () method which is directed to this instance of an object subclass [ superClass.prototype.constructor.call (this) ]

as an object:
a, sub-class instance method, the prototype object point [parent class is defined on the parent class instance methods or properties, can not be called by super ]
b in sub-class static method, [pointing to the parent class in a static method subclass can call the super static methods and static properties of the parent class ]

class inheritance and super keywords related example:

    // parent 
    class the Person { 
        constructor (name, Age) { 
            this .name = name;
             this .age = Age;
             // parent class constructor is invoked if this sub-class constructor is directed in this sub examples of object classes 
            // the console.log (the this); 
        } 
        sayName () { 
            the console.log ( 'examples of the method in Person', the this .name); 
        } 

        static say () { 
            the console.log ( static 'in Person method ' ) 
        } 
    } 

    Person.staticAttr =' static properties in the Person ' ; 

    // inherited 
    class Chinese extends Person {
        constructor (name, Age, skinColor) {     
            Super (name, Age);     // call the parent class constructor sub-class constructor 
            // in the super () with under the this 
            the this .skinColor = skinColor; 
        } 
        
        static fun1 () { 
            // this point is inside the super parent 
            super.say (); 
            super.staticAttr 
        } 

    } 

    the let C = new new Chinese ( 'Bob', 18 is, 'Yellow' );
     // prototype inheritance, 
    c.sayName ();
     // class inherits 
    Chinese.say (); 
    console.log (Chinese.staticAttr); 
    
    Chinese.fun1 () // static method Person Person in static properties
    

    // subclass inherits the parent class inherit [static properties and methods] 
    the console.log (Chinese .__ === proto__ the Person);     // to true 

    // prototype object subclasses inherits from the parent class prototype object inherits properties and methods [example] 
    console.log (Chinese.prototype .__ proto__ Person.prototype ===);     // to true
Inheritance super class and use examples

 

Guess you like

Origin www.cnblogs.com/nzcblogs/p/11373926.html