class-基本语法(es6)

2019-06-25  本文已影响0人  飞奔的小白

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#,却失去了它的本性和个性

上一篇 下一篇

猜你喜欢

热点阅读