js构造函数
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#,却失去了它的本性和个性
网友评论