class基本用法

121 阅读1分钟
  • 类的简介:类本身就是构造函数

  • constructor是类默认的方法,类生成的实例自动调用该方法

  • 类的实例生成与es5完全一样

  • 与es5不同点:类内部定义的属性是不可枚举的

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

    } var point = new Point(2,4);

    Object.keys(Point.prototype) // [] Object.getPropertyName(Point.prototype) // ['constructor','toString']

    point.hasOwnProperty('x') // false point.hasOwnProperty('y') // false point.hasOwnProperty('toString') // false point.proto.hasOwnProperty('toString') //false

  • 属性表达式:类的属性名可以用表达式

    let methodName = 'getArea'; class square{ constructor(){...} methodName{...} }