inheritprototype原型继承封装及综合继承最简实例
1、inheritprototype.js
;(function(){
var s = {
inheritObject:function(o){//对象继承封装
var F = function(){};
F.prototype = o;
return new F();
},
inheritPrototype:function(subclass,supperclass){//原型继承封装
var obj = this.inheritObject(supperclass.prototype);
obj.constructor = subclass;
subclass.prototype = obj;
}
};
window.$ = window.s = s;//起别名并把闭包内的命名空间对象s暴露出去
})(window);
2、inheritprototype.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/inheritprototype.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
;(function(){
function Shape(){//超类
this.name = 'Lucy';
this.type = '超体者'
}
Shape.prototype = {
init:function(){
var name = this.getname();
var type = this.gettype();
console.log(name);
console.log(type);
},
getname:function(){
return this.name;
},
gettype:function(){
return this.type;
}
}
function Triangle(){//子类
Shape.apply(this);//this继承构造体
this.sex = '女';
}
$.inheritPrototype(Triangle,Shape);//这里一定要注意,是先继承再拓展自己的原型方法,否则报错
Triangle.prototype.getsex = function(){
console.log(this.sex);
}
var o = new Triangle();
o.init();//继承父元素的init()并执行
o.getsex();//执行Triangle构造函数的方法
})();
</script>
</html>
最新文章
- 七种机器内部排序的原理与C语言实现,并计算它们的比较次数与移动次数。
- JavaScript 面向对象(二) —— 案例篇
- Java程序员要求具备的10项技能
- 使用squid配置透明代理并对上网行为进行控制
- iOS系统消息
- Spring第一天
- 如何安装nodejs
- ios kaifa
- [iOS]把16进制(#871f78)颜色转换UIColor
- Django数据操作
- 版本控制工具git入门
- linux里忘记root密码解决办法
- 【转】android TV CTS 4.0.3_r1测试
- Demo 示例控制输入光标位置
- 查看文章strncpy()功能更好的文章
- 201521123115《Java程序设计》第6周学习总结
- 设计模式——状态模式(C++实现)
- BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离+前缀和
- C#使用ES
- 以ActiveMQ为例JAVA消息中间件学习【4】——消息中间件实际应用场景