2014-10-06 110 views
0
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
var animal = { 
    type: "mammal" 
}; 

var dog = { 
    sound: "bark" 
}; 

dog.extends(animal); 
alert(dog.type); 
}); 
</script> 

上面的代碼沒有提醒,並且錯誤地指出未定義不是函數。任何想法爲什麼?Javascript擴展不起作用

+3

對象不具有的擴展方法 – 2014-10-06 07:03:43

+0

可能http://jsfiddle.net/arunpjohny/t4c46dzz/1/ – 2014-10-06 07:04:49

回答

3

試試這個

$(document).ready(function(){ 
    var animal = { 
     type: "mammal" 
    }; 

    var dog = { 
     sound: "bark" 
    }; 

    $.extend(dog, animal); 
    alert(dog.type); 
    }); 

http://jsfiddle.net/auekn4f1/

+0

是啊,這個工程。謝謝:)但是,我已經在我的代碼中擴展的方式,是不是另一種方式? – 2014-10-06 07:06:14