代碼如下:
<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>