ES6 --- class of static methods, static and instance properties

Preface:

  Examples of the class corresponding to the prototype, all methods defined in the class, instance will be inherited.

 

main body:

If the previous method, coupled with the static keyword, it means that the method can not be inherited instance , but directly through the class to call , which is called "static method." 
But pay attention to the parent class static methods, also inherited by subclasses

 

Note: When invoked with the instance will complain

TypeError: dad.habit is not a function

 

(2) static method can also be called from the super objects

 

 

 

 

(3) static properties  

Static Class attribute refers to the attribute itself, i.e. Class.propname, rather than defining the instance object ( the this property on) a

  

 

    The above wording as Foo class defines a static properties prop.
  Currently, only such an approach is feasible because ES6 clear that internal Class only static methods, static properties no

  

 

 

 

Examples of attributes (4) Class

  ES7 has proposed a static property, currently Babel transcoder support. The proposal for instance properties and static properties, provide for a new wording.

  Class instance attributes may be by the equation, defined in the class is written.  

  

 

   The above code, myProp are examples of attributes of MyClass. In the instance of MyClass, you can read this property.
  Previously, we define an instance property can only be written on the inside constructor method of the class.

  

 

   In the above code, the constructor constructor which defines this.state properties.
  With the new wording in the future, there may not be defined constructor method.  

   这个新写法大大方便了静态属性的表达

  

小结:
上面代码中, 老写法的静态属性定义在类的外部。 整个类生成以后, 再生成静态属性。
这样让人很容易忽略这个静态属性, 也不符合相关代码应该放在一起的代码组织原则。
另外, 新写法是显式声明( declarative), 而不是赋值处理, 语义更好。

 

   

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/12343220.html