class与prototype

Create an instance of an object:

ES5 commonly used model constructor

function Person(name){
    this.name = name;
    
    this.getName = function(){
        return this.name
    }
}

Capture .PNG


By defining classes class ES6

class Person{
    constructor(name){
        this.name = name;
    }

    getName(){
        return this.name;
    };
}

Capture .PNG

1, all classes of methods are defined in the class prototype property above

2, methods, properties, classes and the prototype is not enumerable, so only through Object.getOwnPropertyNames whose properties (Person / Person.prototype), the method name to traverse;

     Note: for-in and Object.keys () can only get enumerable properties, methods


reference:

http://es6.ruanyifeng.com/#docs/class


Guess you like

Origin blog.51cto.com/8898126/2423317