萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js對象的復制繼承實例

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>
copyright © 萬盛學電腦網 all rights reserved