传统的写法javascript
function Point(x, y) { this.x = x this.y = y } Point.prototype.toString = () => { return `( ${this.x}, ${this.y} )` }
ES6为了更接近面向对象的编程方式,提供了Class的概念java
class Point { constructor (x, y) { this.x = x this.y = y } toString { return `( ${this.x}, ${this.y} )` } }
上面定义了一个‘类’,一个构造方法constructor,this是实例对象,一个toString方法(注意没有function这个保留字)编程
class类中的方法(除constructor以外)其实都定义在类的prototype属性上函数
// 好比toString(),至关于 Point.prototype = { toString(){} } // 因此类的新方法能够这样添加进去 Object.assign(Point.prototype, { toString(){}, toValue(){} }) Point.prototype.constructor === Point // true
经过new命令生成对象实例时都默认自动调用constructor
方法,一个类必须有constructor
方法,没有显式定义的话,一个空的con
方法会被默认添加this
constructor () {}
该方法默认返回实例对象(就是this),可是能够指定返回另外一个对象prototype
class Foo { constructor { return Object.create(null) } } new Foo() instanceof Foo // false
实例对象的建立同以前同样code
class Point { // 这里为了缩小工做量,没有所有书写 constructor () {} tostring () {} } let point = new Point(2, 3) point.toString() // (2, 3) point.hasOwnProperty('x') // true,由于该实例属性显式的定义在this上 point.hasOwnProperty('toString') // false,由于方法都是定义在原型上的 point.prototype.hasOwnProperty('toString') // true point._proto_ === Point.prototype // true
ES6的Class只是ES5的构造函数的一层包装,因此继承了函数的许多属性,包括name属性对象
class Point {} Point.name // 'Point'
class不存在变量提高继承
new Foo() // ReferenceError class Foo {} // let Foo = class {} // class Bar extends Foo {} // 由于class没有提高,因此上面代码不会报错
class childPoint extends Point { constructor(x, y, color) { super(x, y) this.color = color } toString() { return this.color + '' + super.toString() // 等于Parent.toString() } }
子类必须在constructor方法中调用super方法,不然新建实例会出错。另外只有调用super以后,才可使用this关键字ip
class Point{} class childPoint extends Point{ constructor (x, y, color) { this.color = color // ReferenceError super(x, y) this.color = color // 正确 } } let cp = new childPoint() // ReferenceError
子类的_proto_属性
class B extends A { } B._prototype_ === A // true B.prptotype._proto_ === A.prototype // true Object.getPrototypeof(B) === A // true
getter和setter
class MyClass { get prop() { return 'getter' } set prop(value) { console.log('setter:' + value ) } } let inst = new MyClass() inst.prop = 123 // setter: 123 inst.prop // 'getter'
Class的静态方法,在一个方法前面,加上static关键字,就表示该方法不会被实例继承,可是能够被子类继承
class Foo { static classMethod () { return 'hello' } } Foo.classMethod() // hello let foo = new Foo() foo.classMethod() // TypeError: undefined is not a function // 可是能够被子类继承,也可从super上调用 class Bar extends Foo { // static classMethod() { // return super.classMethod() + ', too' // } } Bar.classMethod() // hello