转载自: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);
网友评论