JavaScript继续

1、原型链继承

function Parent() {
    this.name = 'Mike'
}
function Child() {
    this.age = 12;
}
Child.prototype = new Parent();//Child继承Parent,经由过程原型,构成链条

var test = new Child();
console.log(test.age);
console.log(test.name);//获得被继承的属性

//继承原型链继承
function Brother() {//brother组织
    this.weight = 60;
}
Brother.prototype = new Child();//继承原型链继承
var brother = new Brother();
console.log(brother.name);
console.log(brother.age);

2、借用组织函数(类式继承)

function Parent(age) {
    this.name = ['Mike', 'Bill', 'Andy'];
    this.age = age;
}
function Child(age) {
    Parent.call(this, age);//把this指向Parent,同时还能够通报参数
}
var test = new Child(21);
console.log(test.name);
console.log(test.age);
test.name.push('Bill');
console.log(test.name);

3、组合继承

function Parent(age) {
    this.name = ["Mike", "Jack", "Bill"];
    this.age = age;
}
Parent.prototype.run = function () {
    return this.name + " are both " + this.age;
};
function Child(age) {
    Parent.call(this, age);//对象假装,给超范例传参
}
Child.prototype = new Parent();//原型链继承
var child = new Child(12);//写new Parent(12)也行
console.log(child.run() + " years old.");

4、原型式继承

function obj(o) {
    function F() {
    }

    F.prototype = o;
    return new F();
}
var box = {
    name: "Andy",
    arr: ["brother", "sister", "father"]
};
var b1 = obj(box);
console.log(b1.name);
console.log(b1.arr);

b1.name = "Mike";
console.log(b1.name);

b1.arr.push("mother");
console.log(b1.arr);

var b2 = obj(box);
console.log(b2.name);
console.log(b2.arr);

5、寄生组合式继承

function obj(o) {
    function F() {
    }

    F.prototype = o;
    return new F();
}
function create(parent, test) {
    var f = obj(parent.prototype);//建立对象
    v.constructor = test;//加强对象
}
function Parent(name) {
    this.name = name;
    this.arr = ["brother", "sister", "father"]
}
Parent.prototype.run = function () {
    return this.name;
};
function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
}
Child.prototype = new Parent();

var test = new Child("Andy", 12);
console.log(test.run());

test.arr.push("Jack");
console.log(test.arr);
console.log(test.run());//Andy,只同享了要领

var test2 = new Child("Bill", 21);
console.log(test2.arr);//援用问题解决
    原文作者:whjin
    原文地址: https://segmentfault.com/a/1190000009016071
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞