Class类

作者: 天字一等 | 来源:发表于2018-08-28 19:20 被阅读4次

转载自:http://es6.ruanyifeng.com/#docs/class
//定义类

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}
var p = new Point(1, 2);
定义了一个类,constructor是构造方法,this代表实例对象,也就是说es5中构造函数Point对应es6中的构造方法constructor.方法之间  不需要加逗号

typeof Point  //function
Point === Point.prototype.constructor  //true
等价于
function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};

var p = new Point(1, 2);

相关文章

网友评论

      本文标题:Class类

      本文链接:https://www.haomeiwen.com/subject/jvyqwftx.html