ES6 class (basic grammar + Method)

Static properties and static methods

1. not class instance attributes and methods have only their own class
2 only by calling class

Static method with the same name as the conventional method, do not conflict with
the static keyword (static method)

Static properties
. Class name attribute name = attribute value;


 

Static properties Application examples:

    // occupational 
    class Profession { 

    } 

    class Character { 
        constructor (PFS) { 
            the this .pfs = PFS; 
        } 
    } 
    // static properties do arranged 
    Character.config = { 
        Profession: {
             'Conjurer': 1 ,
             'Archer' : 2 
        } 
    } 
    // create an instance of the class 
    new new Character (Character.config.profession [ 'Conjurer']);

Application examples static method

    {the Person class
         // static methods 
        static the format (Programmer) { 
            programmer.haveGirlFriend = to true ; 
            programmer.hair = to true ; 
        } 
    } 
    // programmer category 
    class Programmer { 
        constructor () { 
            the this .haveGirlFriend = to false ;
             the this .hair = to false ; 
        } 
    } 
    // the class instance of a programmer into the general category 
    const = programmer new new programmer (); 
    Person.format (programmer); 
    the console.log (programmer);

Expressions like
P can only be accessed from within the class
is the class of its own

    the Person = const class P { 
        constructor () { 
            Pa =. 1 ; 
            the console.log (P === the Person); 
            the console.log ( 'I dove hand !! Gugu Gu !!' ); 
        } 
    } 

    new new the Person () ; 

    // automatically performed 
    const of Person1 = new new class P { 
        constructor () { 
            Pa =. 1 ; 
            the console.log ( 'I dove hand !! Gugu Gu !!' ); 
        } 
    } ();

getter setter
similar properties to provide hooks
to do some extra things get property values and settings when the property value


 

Getters the ES5 / the setter
1. Writing get / set methods in the object literals

    const obj = {
        _name: '',

        get name() {
            console.log('123');
            return this._name;
        },

        set name(val) {
            this._name = val;
        }
    }
    obj.name = 222;
    console.log(obj);

2. Object.defineProperty

    var obj = {
        _name: ''
    };
    Object.defineProperty(obj, 'name', {
        get: function() {
            console.log('正在访问name');
            return this._name;
        },
        set: function(val) {
            console.log('正在修改name');
            this._name = val;
        }
    });
    obj.name = 10;
    console.log(obj.name);

ES6 writing:

    {the Person class 
        constructor () { 
            the this ._name = '' ; 
        } 
        GET name () { 
            console.log ( 'is available for name' ); 

            return `My name is {$ the this ._name`}; 
        } 
        the SET name (Val ) { 
            the console.log ( 'being modified name' ); 

            the this ._name = Val; 
        } 
    } 
    const Person = new new the Person (); 
    PERSON.NAME = 'Ace' ; 
    the console.log (PERSON.NAME);
    class AudioPlayer {
        constructor() {
            this._status = 0;
            this.status = 0;
            this.init();
        }
        init() {
            const audio = new Audio();
            audio.src = '....';
            audio.oncanplay = () => {
                audio.play();
                this.status = 1;
            }
        }
        get status() {
            return this._status;
        }
        set status(val) {
            STATUS_MAP const = {
                 0: 'pause' ,
                 1: 'play' ,
                 2: 'Loading' 
            }; 
            // change the copy buttons 
            . document.querySelector ( 'App .play-BTN #') the innerText = STATUS_MAP [Val ];
             the this ._status = Val; 
        } 
    } 
    const Audio = new new the AudioPlayer ();

name class name

    class Humen {

    }
    console.log(Humen.name);//Humen

    const Humen = class P{

    }
    console.log(Humen.name);//P

new.target points behind the new keyword class

    class Car {
        constructor() {
            console.log(new.target);
        }
    }
    new Car();

Syntactic sugar

    function Car () {
         IF (! ( the this  the instanceof Car)) {
             the throw Error ( 'call must use the new key Car' ); 
        } 
    } 
    new Car ();

In Simulations es5:
constructor
1. Create an empty object
2. prototype property constructor as a prototype empty object
3. The empty object of this assignment
4 Executive Functions
5. If the function does not return this value is returned [return before the null object]

    function Person(name, age) {
        this.name = name;
        this.age = age;
    }
    console.log(new Person('张三', 11));
    function the Constructor (fn, args) {
         // object created as a prototype to fn 
        var _this = the Object.create (fn.prototype);
         // execute a function passing the parameter 
        var RES = fn.apply (_this, args);
         return RES ? RES: _this; 
    } 
    function the Person (name, Age) {
         the this .name = name;
         the this .age = Age; 
    } 
    Person.prototype.say = function () { 
        console.log ( 'my name is' + the this .name) ; 
    } 
    var person = Constructor(Person, ['张三', 12]);
    console.log(person);

Guess you like

Origin www.cnblogs.com/chenyingying0/p/12164093.html