继续的实质便是复制,即重写原型工具,代之以一个新类型的实例。
function SuperType() { this.property = true;}SuperType.prototype.getSuperValue = function() { return this.property;}function SubType() { this.subproperty = false;}// 这里是关键,创建SuperType的实例,并将该实例赋值给SubType.prototypeSubType.prototype = new SuperType(); SubType.prototype.getSubValue = function() { return this.subproperty;}var instance = new SubType();console.log(instance.getSuperValue()); // true复制代码
原型链方案存在的缺陷:多个实例对引用类型的操作会被修改。

function SuperType(){ this.colors = ["red", "blue", "green"];}function SubType(){}SubType.prototype = new SuperType();var instance1 = new SubType();instance1.colors.push("black");alert(instance1.colors); //"red,blue,green,black"var instance2 = new SubType(); alert(instance2.colors); //"red,blue,green,black"复制代码
2、借用布局函数继续
利用父类的布局函数来增强子类实例,等同于复制父类的实例给子类(不该用原型)
function SuperType(){ this.color=["red","green","blue"];}function SubType(){ //继续自SuperType SuperType.call(this);}var instance1 = new SubType();instance1.color.push("black");alert(instance1.color);//"red,green,blue,black"var instance2 = new SubType();alert(instance2.color);//"red,green,blue"复制代码
核心代码是SuperType.call(this),创建子类实例时调用SuperType布局函数,于是SubType的每个实例都会将SuperType中的属性复制一份。
缺陷:
只能继续父类的实例属性和方法,不能继续原型属性/方法无法实现复用,每个子类都有父类实例函数的副本,影响性能3、组合继续组合上述两种方法便是组合继续。用原型链实现对原型属性和方法的继续,用借用布局函数技能来实现实例属性的继续。
function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"];}SuperType.prototype.sayName = function(){ alert(this.name);};function SubType(name, age){ // 继续属性 // 第二次调用SuperType() SuperType.call(this, name); this.age = age;}// 继续方法// 构建原型链// 第一次调用SuperType()SubType.prototype = new SuperType(); // 重写SubType.prototype的constructor属性,指向自己的布局函数SubTypeSubType.prototype.constructor = SubType; SubType.prototype.sayAge = function(){ alert(this.age);};var instance1 = new SubType("Nicholas", 29);instance1.colors.push("black");alert(instance1.colors); //"red,blue,green,black"instance1.sayName(); //"Nicholas";instance1.sayAge(); //29var instance2 = new SubType("Greg", 27);alert(instance2.colors); //"red,blue,green"instance2.sayName(); //"Greg";instance2.sayAge(); //27复制代码
缺陷:
第一次调用SuperType():给SubType.prototype写入两个属性name,color。第二次调用SuperType():给instance1写入两个属性name,color。实例工具instance1上的两个属性就屏蔽了其原型工具SubType.prototype的两个同名属性。以是,组合模式的缺陷便是在利用子类创建实例工具时,其原型中会存在两份相同的属性/方法。
4、原型式继续利用一个空工具作为中介,将某个工具直接赋值给空工具布局函数的原型。
function object(obj){ function F(){} F.prototype = obj; return new F();}复制代码
object()对传入个中的工具实行了一次浅复制,将布局函数F的原型直接指向传入的工具。
var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"]};var anotherPerson = object(person);anotherPerson.name = "Greg";anotherPerson.friends.push("Rob");var yetAnotherPerson = object(person);yetAnotherPerson.name = "Linda";yetAnotherPerson.friends.push("Barbie");alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"复制代码
缺陷:
原型链继续多个实例的引用类型属性指向相同,存在修改的可能。无法通报参数其余,ES5中存在Object.create()的方法,能够代替上面的object方法。
5、寄生式继续核心:在原型式继续的根本上,增强工具,返回布局函数
function createAnother(original){ var clone = object(original); // 通过调用 object() 函数创建一个新工具 clone.sayHi = function(){ // 以某种办法来增强工具 alert("hi"); }; return clone; // 返回这个工具}复制代码
函数的紧张浸染是为布局函数新增属性和方法,以增强函数
var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"]};var anotherPerson = createAnother(person);anotherPerson.sayHi(); //"hi"复制代码
缺陷(同原型式继续):
原型链继续多个实例的引用类型属性指向相同,存在修改的可能。无法通报参数6、寄生组合式继续结合借用布局函数通报参数和寄生模式实现继续
function inheritPrototype(subType, superType){ var prototype = Object.create(superType.prototype); // 创建工具,创建父类原型的一个副本 prototype.constructor = subType; // 增强工具,填补因重写原型而失落去的默认的constructor 属性 subType.prototype = prototype; // 指定工具,将新创建的工具赋值给子类的原型}// 父类初始化实例属性和原型属性function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"];}SuperType.prototype.sayName = function(){ alert(this.name);};// 借用布局函数通报增强子类实例属性(支持传参和避免修改)function SubType(name, age){ SuperType.call(this, name); this.age = age;}// 将父类原型指向子类inheritPrototype(SubType, SuperType);// 新增子类原型属性SubType.prototype.sayAge = function(){ alert(this.age);}var instance1 = new SubType("xyc", 23);var instance2 = new SubType("lxy", 23);instance1.colors.push("2"); // ["red", "blue", "green", "2"]instance1.colors.push("3"); // ["red", "blue", "green", "3"]复制代码
这个例子的高效率表示在它只调用了一次SuperType 布局函数,并且因此避免了在SubType.prototype 上创建不必要的、多余的属性。于此同时,原型链还能保持不变;因此,还能够正常利用instanceof 和isPrototypeOf()
这是最成熟的方法,也是现在库实现的方法
7、混入办法继续多个工具function MyClass() { SuperClass.call(this); OtherSuperClass.call(this);}// 继续一个类MyClass.prototype = Object.create(SuperClass.prototype);// 稠浊其它Object.assign(MyClass.prototype, OtherSuperClass.prototype);// 重新指定constructorMyClass.prototype.constructor = MyClass;MyClass.prototype.myMethod = function() { // do something};复制代码
Object.assign会把 OtherSuperClass原型上的函数拷贝到 MyClass原型上,使 MyClass 的所有实例都可用 OtherSuperClass 的方法。
8、ES6类继续extendsextends关键字紧张用于类声明或者类表达式中,以创建一个类,该类是另一个类的子类。个中constructor表示布局函数,一个类中只能有一个布局函数,有多个会报出SyntaxError缺点,如果没有显式指定布局方法,则会添加默认的 constructor方法,利用例子如下。
class Rectangle { // constructor constructor(height, width) { this.height = height; this.width = width; } // Getter get area() { return this.calcArea() } // Method calcArea() { return this.height this.width; }}const rectangle = new Rectangle(10, 20);console.log(rectangle.area);// 输出 200-----------------------------------------------------------------// 继续class Square extends Rectangle { constructor(length) { super(length, length); // 如果子类中存在布局函数,则须要在利用“this”之前首先调用 super()。 this.name = 'Square'; } get area() { return this.height this.width; }}const square = new Square(10);console.log(square.area);// 输出 100复制代码
extends继续的核心代码如下,实在现和上述的寄生组合式继续办法一样
function _inherits(subType, superType) { // 创建工具,创建父类原型的一个副本 // 增强工具,填补因重写原型而失落去的默认的constructor 属性 // 指定工具,将新创建的工具赋值给子类的原型 subType.prototype = Object.create(superType && superType.prototype, { constructor: { value: subType, enumerable: false, writable: true, configurable: true } }); if (superType) { Object.setPrototypeOf ? Object.setPrototypeOf(subType, superType) : subType.__proto__ = superType; }}复制代码
总结
1、函数声明和类声明的差异
函数声明会提升,类声明不会。首先须要声明你的类,然后访问它,否则像下面的代码会抛出一个ReferenceError。
let p = new Rectangle(); // ReferenceErrorclass Rectangle {}复制代码
2、ES5继续和ES6继续的差异
ES5的继续本色上是先创建子类的实例工具,然后再将父类的方法添加到this上(Parent.call(this)).ES6的继续有所不同,本色上是先创建父类的实例工具this,然后再用子类的布局函数修正this。由于子类没有自己的this工具,以是必须先调用父类的super()方法,否则新建实例报错。