Pay attention to several points:
- In the
class
method defined in both prototyping
class F{
say(){}
}
F.prototype.hasOwnProperty('say') //true
class
Defined attributes, are examples of properties, rather than on the prototype, and is configured the same as defined in the function attributes overrideclass
definition
class W {
a = 1
constructor(){
this.a = 2
}
}
let w = new W()
w.hasOwnProperty('a') //true
w.a // 2
- Point constructor class itself
Object.getPrototypeOf(w).constructor === W
class
Did not write a constructor, it will automatically create one, and returnsthis
- The
es5
difference is that the method defined in the class is not enumerated,es5
theprototype
wording may be
//class
class F {
say(){}
}
Object.keys(F.prototype) // []
// es5
function G() {}
G.prototype.say = function(){}
Object.keys(G.prototype) // ['say']
__proto__
Itself is not a characteristic of language, which is added when the major manufacturers to achieve specific private property, we can use Object.getPrototypeOfstatic
Examples of modified methods are not inherited, static methods can only be invoked class, you can duplicate names and instance methods
class H {
static say(){
console.log(1)
}
say(){
console.log(2)
}
}
let h = new H()
h.say() // 2
H.say() // 1
- Use
extends
when inheritance if the subclass explicitly write a constructor, the constructor must be calledsuper()
, did not explicitly write the subclasses constructor is not class
You can writeget
,set
class K{
get a(){
return this.b
}
set a(val){
this.b = val
}
}