/**
* 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********************************/
js几种继承的fa
最新推荐文章于 2024-07-26 11:41:15 发布