JavaScript 定义类
ES6以前:
function Point(x, y) { this.x = x; this.y = y; } Point.prototype.hello= function () { return '(' + this.x + ', ' + this.y + ')'; }; var p = new Point(1, 2);
ES6提供了class关键字:
//定义类 class Point { constructor(x, y) { this.x = x; this.y = y; } hello() { return '(' + this.x + ', ' + this.y + ')'; } }