本文实例讲述了js对象的复制继承。分享给大家供大家参考。具体如下:
复制代码 代码如下:
<script type="text/javascript">
Object.prototype.extend = function(obj){
//在函数里,把obj属性复制到自身
for(var k in obj){
if(obj.hasOwnProperty(k)){
if(this[k] == undefined){
this[k] = obj[k];
}
}
}
}
var kitty = {color:'yellow',climb:function(){alert('我会爬树');}};
var tiger = {color:'yellow and black'};
tiger.extend(kitty);
tiger.climb();
</script>
Object.prototype.extend = function(obj){
//在函数里,把obj属性复制到自身
for(var k in obj){
if(obj.hasOwnProperty(k)){
if(this[k] == undefined){
this[k] = obj[k];
}
}
}
}
var kitty = {color:'yellow',climb:function(){alert('我会爬树');}};
var tiger = {color:'yellow and black'};
tiger.extend(kitty);
tiger.climb();
</script>
希望本文所述对大家的javascript程序设计有所帮助。