JS 语法前提
面向对象实现无非三个特征,封装,继承,多态。
JS 实现基于构造函数(constructor)和原型链(prototype)。
JavaScript 引擎先寻找对象本身的属性,如果找不到,就到它的原型去找,如果还是找不到,就到原型的原型去找。
关于 this 知识:
- 创建一个空对象,作为将要返回的对象实例。
- 将这个空对象的原型,指向构造函数的 prototype 属性。
- 将这个空对象赋值给函数内部的 this 关键字。
- 开始执行构造函数内部的代码。
- if Constructor returns object, instanceObject will get the object, or else get ‘this object’.
JS 实现
构造函数实现封装:
1 2 3 4 5 6 7 8 9 10 11 12 13
| function Person(name, age) { this.name = name this.age = age }
Person.prototype.type = 'human'
Person.prototype.sayName = function() { console.log(this.name) }
var instanceObject = new Person('张三', 22)
|
实现继承:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| function Student (name, age, gender) { Person.call(this, name, age) this.gender = gender }
Student.prototype = Object.create(Person.prototype)
|| Student.prototype.print = function() { Person.prototype.print.call(this) }
Student.prototype.constructor = Student
Student.prototype.method = '...'
var instanceObject = new Student('张三', 22, 'man')
|
实现多重继承 Mixin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| function M1() { this.hello = 'hello' } function M2() { this.world = 'world' } function S() { M1.call(this) M2.call(this) }
S.prototype = Object.create(M1.prototype)
Object.assign(S.prototype, M2.prototype)
S.prototype.constructor = S
var s = new S() s.hello s.world
|
ES6 实现
可以看作是 Syntactic sugar
类的声明:
1 2 3 4 5 6 7 8 9 10
| class Person { constructor(name, age) { this.name = name this.age = age } sayName() { console.log(this.name) return this.age } }
|
继承的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Student extends Person { constructor(name, age, gender) { super(name, age) this.gender = gender } toString() { return this.gender + ' ' + super.sayName() } }
var instanceObject = new Student('张三', 22, 'man')
|