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 + ')';
  }
}

 

posted @ 2016-12-07 10:12  没离开过  阅读(138)  评论(0编辑  收藏  举报