js構(gòu)造函數(shù)
function MathHandle(x,y){
this.x = y;
this.y = y;
}
MathHandle.prototype.add = function(){
return this.x + this.y
}
var m = new MathHandle(1,2)
console.log(m.add());
Class 語法
class MathHandle{
constructor(x,y){
this.x = x;
this.y = y;
}
add() {
return this.x + this.y
}
}
var m = new MathHandle(1,2);
console.log(m.add())
語法糖
class MathHandle{
// ...
}
typeof MathHandle //function
MathHandle === MathHandle.prototype.constructor //ture
m.__proto__ === MathHandle.prototype //ture
這種語法糖形式狰住,看起來和實際原理不一樣的東西焕参,我個人不太贊同
形式上強行模仿 java C#轻纪,卻失去了它的本性和個性