1. Instance properties, static properties, and stereotype properties

    Instance attributes are defined on the instance, either on this inside the constructor or on the instance after the constructor has been instantiated.

    Static property, the property defined above the constructor. Can be accessed directly through the constructor.

    Stereotype property, a property defined on a constructor stereotype object.

    Function Person(name){this.name = name; } // Define the static attribute person. sex = 'male' on the constructor; Person.prototype.weight = 20 let Person = new Person('wang'); Person.age = 23; person.age = 23; Log (person.name) // wang // Access instance property age console.log(person.age) // 23 // Access static property sex Console. log(person.sex) // male // Accesses the prototype property weight console.log(person.weight) // 20 // accesses the prototype property weight via the constructor console.log(Person.prototype.weight); / / 20Copy the code
  2. Instance method, static method and prototype method

    Instance methods are defined on an instance, either on this inside the constructor or on an instance after the constructor has been instantiated.

    Static property, the property defined above the constructor. Can be accessed directly through the constructor.

    Stereotype property, a property defined on a constructor stereotype object.

    function Person(name) { this.name = name; SayIn = function() {console.log('my name is ', this.name); // Define instance attribute this.sayin = function() {console.log('my name is ', this.name); Person.eat = function(){console.log(' I can eat anything')}; Person.prototype.run = function(){console.log(' I can run 100m in 9.88s')} let Person = new Person('li'); // Define instance method person.sayout = function(){console.log('my name is out ', this.name); } // Access the instance method sayIn person.sayin (); SayOut person.sayout (); Eat person.eat (); // my name is li. Run person.run(); // I can eat anything. // I can run 100m in 9.88s // Access the prototype method person.prototype.run () // I can run 100m in 9.88sCopy the code
  3. ES5 and ES6, static method, static property declaration difference?

    class Person{ constructor(sex) { this.sex = sex; } static printSex(){console.log('this is printSexMethod'); }} // Static attributes can only pass through classes. Attribute name to declare Person.age = 20; // through the class. PrintSexOut = () => {console.log('this is printSexOut ')} console.log('age', person.age); // 20 Person.printSex(); // this is printSexMethod Person.printSexOut(); // this is printSexMethodOutCopy the code