2012-04-06 19 views

回答

2

prototype是在類中定義的,而不是在JavaScript對象的引用對象,你需要定義一個類,並建立使用prototype繼承:

var animal = {eats:true}; 
function Rabit(){}; 
Rabit.prototype = animal; 
Rabit.prototype.jumps = true; 

var rabit = new Rabit(); 
rabit.jumps; // true 
rabit.eats; // true 

或者,如果你定義好這兩個實體類:

function Animal(){}; 
Animal.prototype.eats = true; 

function Rabit(){}; 
Rabit.prototype = new Animal(); 
Rabit.prototype.jumps = true; 

var rabit = new Rabit(); 
rabit.jumps; // true 
rabit.eats; // true 

有一個無證__proto__對象Gecko瀏覽器,如谷歌Chrome,即鱸你這個傻瓜原型鏈和靜態繼承的OBJE從另一個ct:

var animal = {eats:true}; 
var rabbit = {jumps:true}; 

rabbit.__proto__ = animal; 
rabit.jumps; // true 
rabit.eats; // true 
+0

嘿謝謝..:)\ – 2012-04-06 07:30:25

+0

如果這是您的問題的答案,請將其標記爲已回答 – 2012-05-15 03:05:36