js完成继续的几种体式格局

媒介:大多OO言语都支撑两种继续体式格局:接口继续和完成继续,而ECMAScript中没法完成接口继续,ECMAScript只支撑完成继续,而且其完成继续主如果依托原型链来完成。

1.原型链

基础头脑:应用原型让一个援用范例继续别的一个援用范例的属性和要领。
组织函数,原型,实例之间的关联:每一个组织函数都有一个原型对象,原型对象包括一个指向组织函数的指针,而实例都包括一个指向原型对象的内部指针。

原型链完成继续例子:

function SuperType() {
    this.property = true;
}
SuperType.prototype.getSuperValue = function() {
    return this.property;
}
function subType() {
    this.property = false;
}
//继续了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
    return this.property;
}

var instance = new SubType();
console.log(instance.getSuperValue());//true

2.借用组织函数

基础头脑:在子范例组织函数的内部挪用超类组织函数,经由历程运用call()和apply()要领能够在新建立的对象上实行组织函数。

例子:

function SuperType() {
    this.colors = ["red","blue","green"];
}
function SubType() {
    SuperType.call(this);//继续了SuperType
}
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);//"red","blue","green","black"

var instance2 = new SubType();
console.log(instance2.colors);//"red","blue","green"

3.组合继续

基础头脑:将原型链和借用组织函数的手艺组合在一块,从而发挥二者之长的一种继续情势。

例子:

function SuperType(name) {
    this.name = name;
    this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function() {
    console.log(this.name);
}
function SubType(name, age) {
    SuperType.call(this,name);//继续属性
    this.age = age;
}
//继续要领
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function() {
    console.log(this.age);
}

var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
consol.log(instance1.colors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18

var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"red","blue","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20

4.原型式继续

基础主意:借助原型能够基于已有的对象建立新对象,同时还不必需因而建立自定义的范例。

原型式继续的头脑可用以下函数来申明:

function object(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

例子:

var person = {
    name:"EvanChen",
    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");

console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

ECMAScript5经由历程新增Object.create()要领范例化了原型式继续,这个要领吸收两个参数:一个用作新对象原型的对象和一个作为新对象定义分外属性的对象。

var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"];
};

var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");

var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");

console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

5.寄生式继续

基础头脑:建立一个仅用于封装继续历程的函数,该函数在内部以某种体式格局来加强对象,末了再像真恰是它做了一切事情一样返回对象。

例子:

function createAnother(original) {
    var clone = object(original);
    clone.sayHi = function () {
        alert("hi");
    };
    return clone;
}

var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"

6.寄生组合式继续

基础头脑:经由历程借用函数来继续属性,经由历程原型链的混成情势来继续要领
其基础模子以下所示:

function inheritProperty(subType, superType) {
    var prototype = object(superType.prototype);//建立对象
    prototype.constructor = subType;//加强对象
    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;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
    alert(this.age);
}
    原文作者:Evan_Chen
    原文地址: https://segmentfault.com/a/1190000004730936
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞