寄生组合式继承
来源:互联网 发布:fpga 保存数据 编辑:程序博客网 时间:2024/04/27 20:54
//实现了对o的一次浅复制
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
//subType:子函数名,superType:父函数名。
function inheritPrototype(subType,superType){
var o = object(superType.prototype);
o.constructor = subType;
subType.prototype = o;
}
//父函数,使用构造模式设置变量,原型模式设置方法。
function animal(name) {
this.name = name;
this.data = ["1","2"];
}
animal.prototype.sayName = function() {
alert("我叫"+this.name);
};
//子函数:构造模式
function cat(name,age) {
animal.call(this,name);
this.age = age;
}
//寄生式模式
inheritPrototype(cat,animal);
//原型模式
cat.prototype.action = function() {
alert("我能吃" + this.age + "条鱼");
};
var cat1 = new cat("小花猫",1);
var cat2 = new cat("小肥猫",3);
cat1.sayName(); //我叫小花猫
cat2.sayName(); //我叫小肥猫
cat1.action(); //我能吃1条鱼
cat2.action(); //我能吃3条鱼
cat1.data.push("3");
alert(cat1.data); //1,2,3
alert(cat2.data); //1,2
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
//subType:子函数名,superType:父函数名。
function inheritPrototype(subType,superType){
var o = object(superType.prototype);
o.constructor = subType;
subType.prototype = o;
}
//父函数,使用构造模式设置变量,原型模式设置方法。
function animal(name) {
this.name = name;
this.data = ["1","2"];
}
animal.prototype.sayName = function() {
alert("我叫"+this.name);
};
//子函数:构造模式
function cat(name,age) {
animal.call(this,name);
this.age = age;
}
//寄生式模式
inheritPrototype(cat,animal);
//原型模式
cat.prototype.action = function() {
alert("我能吃" + this.age + "条鱼");
};
var cat1 = new cat("小花猫",1);
var cat2 = new cat("小肥猫",3);
cat1.sayName(); //我叫小花猫
cat2.sayName(); //我叫小肥猫
cat1.action(); //我能吃1条鱼
cat2.action(); //我能吃3条鱼
cat1.data.push("3");
alert(cat1.data); //1,2,3
alert(cat2.data); //1,2
0 0
- javascript 寄生组合式继承
- 寄生组合式继承
- 寄生组合式继承
- 寄生组合式继承
- 寄生组合式继承
- JS寄生组合式继承
- js寄生组合式继承
- JS继承--寄生组合式继承
- JavaScript继承,寄生组合式继承
- JavaScript 继承---寄生组合式继承
- javascript中寄生组合式继承
- JavaScript的寄生组合式继承
- js继承6-----寄生组合式继承
- JavaScript的继承--寄生组合式继承
- JavaScript中的寄生组合式继承方式
- JavaScript中寄生组合式继承的理解
- 【代码整理】JavaScript的寄生组合式继承
- JavaScript对寄生组合式继承的理解
- 底价团购潮 proscenic扫地机器人京东热卖
- Many-to-One映射
- 可编辑下拉框
- 游戏中的名字随机生成代码
- spring 文件夹扫描
- 寄生组合式继承
- this与super的基本用法与基本区别
- NYOJ-252 01串
- java中Int String类型转换
- pulltorefresh扩展下拉上拉刷新加载及监听listview滚动方向
- YV12 and NV12异同,
- arguments
- Java 线程 Runnable和Thread的区别
- DP求连续最长面积一维&&二维