es6 class grammar points to note

Pay attention to several points:

  • In the classmethod defined in both prototyping
class F{
	say(){}
}
F.prototype.hasOwnProperty('say') //true
  • classDefined attributes, are examples of properties, rather than on the prototype, and is configured the same as defined in the function attributes override classdefinition
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 es5difference is that the method defined in the class is not enumerated, es5the prototypewording 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.getPrototypeOf
  • staticExamples 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 extendswhen inheritance if the subclass explicitly write a constructor, the constructor must be called super(), did not explicitly write the subclasses constructor is not
  • classYou can write get,set
class K{
	get a(){
		return this.b
	}
	set a(val){
		this.b = val
	}	
}

Guess you like

Origin blog.csdn.net/zdhanunity/article/details/93205215