js几种继承的fa

/**
 * Created by wolf on 16/10/1.
 */

/********************************原型链继承 start********************************/
function SuperType() {
    this.property = true;
}
SuperType.prototype.getSurperValue = function () {
    return this.property;
}

function SubType() {
    this.subpropety = false;
}
//继承SurperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function () {
    return this.subpropety;
}

var instance = new SubType();
alert(instance.getSurperValue());
alert(instance.getSubValue());

/******************************** 原型链继承end ********************************/

/******************************** 构造函数继承start ********************************/
function Father() {
    this.colors = ["red", "blue", "greed"];
}
function Son() {
    Father.call(this);
}
var son = new Son();
son.colors.push("yellow");
alert(son.colors);
var son1 = new Son();
alert(son1.colors);
/******************************** 构造函数继承end ********************************/
/******************************** 组合继承start ********************************/
function SurperObj(name) {
    this.name = name;
    this.colors = ["red", "blue", "greed"];
}
SurperObj.prototype.sayName = function () {
    alert(this.name)
}
function SonObj(name, age) {
    SurperObj.call(this, name);
    this.age = age;
}
SonObj.prototype = new SurperObj();
SonObj.prototype.constructor = SonObj;
SonObj.prototype.sayAge = function () {
    alert(this.age);
}

var sonObj = new SonObj("wolf", 23);
sonObj.colors.push("nimei");
alert(sonObj.colors);
sonObj.sayName();
sonObj.sayAge();

var sonObj2 = new SonObj("liaobin", 24);
alert(sonObj2.colors);
sonObj2.sayName();
/******************************** 组合继承 end********************************/
/******************************** 原型式继承 start********************************/
function object(o) {
    function F() {
    }

    F.prototype = o;
    return new F();
}
var person = {
    name: "NiDaYe",
    friends: ["SB1", "SB2", "Sb3"]
};
var anoPerson = object(person);
anoPerson.name = "DAYEDE";
anoPerson.friends.push("SB4");
alert(anoPerson.friends);
/******************************** 原型式继承 end********************************/
/******************************** ECMASCript5 提供的Object.create()继承 start********************************/
var o = {
    name: "ECMASCript5",
    friends: ["SB1", "SB2", "Sb3"]
};
var oo = Object.create(o);
oo.name = "ooName";
oo.friends.push("sb55");
alert(oo.friends); //"SB1", "SB2", "Sb3","sb55"
alert(o.friends);   //"SB1", "SB2", "Sb3","sb55"

/******************************** ECMASCript5 提供的Object.create()继承 end********************************/
/******************************** 寄生式继承 start********************************/
function createAnother(org) {
    var clone = object(org);
    clone.sayHi = function () {
        alert("Hi");
    }
    return clone;
}
var org = {
    name: "orgName"
};
var orgObj = createAnother(org);
orgObj.sayHi(); //Hi
/******************************** 寄生式继承 end********************************/
/******************************** 寄生组合继承 start********************************/
function inheritPrototype(subColor, superColor) {
    var prototype = Object(superColor.prototype);
    prototype.prototype = subColor;
    subColor.prototype = prototype;
}

function SuperColor(name) {
    this.name = name;
    this.colors = ["red", "blue", "green"];
}

SuperColor.prototype.sayName = function () {
    alert(this.name);
}
function SubColor(name, age) {
    SuperColor.call(this, name);
    this.age = age;
}

inheritPrototype(SubColor, SuperColor);

SubColor.prototype.sayAge = function () {
    alert(this.age);
}

/******************************** 寄生组合继承 end********************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值