constructor()方法

版权声明:当前文章版权为本人所有,转载请提前联系本人,并附以地址说明。 https://blog.csdn.net/D_claus/article/details/87863080

ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板。 

class Point {
  constructor(x, y) {
    this.x = x;//this指当前实例
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}

constructor方法,这就是构造方法,而this关键字则代表实例对象。也就是说,ES5 的构造函数Point,对应 ES6 的Point类的构造方法。

猜你喜欢

转载自blog.csdn.net/D_claus/article/details/87863080