inheritprototype原型继承封装及综合继承最简实例
2016-05-17 14:47
666 查看
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>
;(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>
相关文章推荐
- phpstorm添加laravle语法支持
- telnet smtp 发送邮件
- php 上传csv文件
- PHP中的11个魔术方法总结:__construct,、__destruct、__call等
- XMl入门介绍及php操作XML
- php getimagesize()函数获取图片宽度高度
- eaccelerator+PHP5.4导致smarty更新模板500报错
- ContentProvider使用例子
- XMl入门介绍及php操作XML
- php-5.3.27安装
- phpwind9.0 顶部和底部版权信息永久性修改
- PHP远程下载图片到本地
- php魔术方法总结
- setPadding()无效的原因分析
- phpstorm10.0.1和webstorm11注册
- laravel5.2、thinkphp5、thinkphp3.2.3性能AB测试
- EditPlus注册码在线生成,强大
- 七牛EVM云主机ftp环境搭建以及注意事项
- 一款不错的PHP在线文件管理系统,PHP WEBFTP
- php 利用远程图片创建新图片完善(jpg,png,gif)