2013-11-25 106 views
0

我有2類:動物(父母)和狗(動物的'孩子'), 當我創建一個動物的對象,並嘗試提醒動物的名字, 我得到了undefined,而不是她的真名。爲什麼? (約雙崗抱歉)JavaScript的繼承

function Animal(name, year){ 
    alert(name); 
    this.name = name; 
    this.year = year; 

    this.age = function(){ 
     var n = new Date().getFullYear(); 
     return n - this.year; 
    }; 
} 

function Dog(name, year, color, type) { 
    this.color = color; 
    this.type = type; 
    Animal.call(this, name, year); 

    //override method age of animal 
    this.age = function(){ 
     var n = new Date().getFullYear(); 
     return (n - this.year) * 7; 
    }; 

    Dog.prototype.age(); 
} 

Dog.prototype = new Animal(); 

(這個js命名的類:JsClass.js)

和HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <script src="JsClass.js"></script> 

     <script type="text/javascript"> 
      var p1 = new Animal("rex",2008); 
     </script> 
     <title>Insert title here</title> 
    </head> 
    <body> 
    </body> 
</html> 

(使用Eclipse)

謝謝!

當我創建動物的對象,並嘗試以提醒動物的名字
+0

作爲參考,通常在設置自己的字段之前調用父構造函數,而不是之後。不過,不應該導致你看到的問題。 – cHao

+0

現在我看到,如果我刪除這一行: Dog.prototype = new Animal(); 它提醒動物的名字。那麼這條線有什麼問題?我應該在哪裏放? – user2097810

回答

1

,我不確定,而不是她的真名

你的警報功能是在動物的構造函數。 當您將其實例化爲Dog的原型時,您沒有將任何事傳遞到該函數

這裏發生的事情:

Dog.prototype = new Animal();調用不帶任何參數的構造函數(因此未定義的警報),並將狗的動物實例的原型。

new Animal("rex", 2008);再次調用構造函數。這一次,用一個名字,所以你會得到「雷克斯」。

我建議將方法設置爲原型的屬性。這樣,函數就不會在每個實例上創建。我會改變你的代碼看起來像這樣:

function Animal(name, year) { 
    this.name = name; 
    this.year = year; 
}; 

Animal.prototype.age = function() { 
    var n = new Date().getFullYear(); 
    return n - this.year; 
}; 

function Dog(name, year, color, type) { 
    Animal.call(this, name, year); 
    this.color = color; 
    this.type = type; 
}; 

// Override method age of animal 
Dog.prototype.age = function() { 
    var n = new Date().getFullYear(); 
    return (n - this.year) * 7; 
}; 

Dog.prototype = new Animal(); 

但還有一個問題 - 你調用構造動物兩次。一旦你把它設置成狗的原型,另一個當你做Animal.call()。解決這個問題的一個方法是使用Crockford的method of inheritance

+0

謝謝。但我應該在這一行插入哪些參數: Dog.prototype = new Animal('wow'); ? – user2097810

+1

應該只是Dog.prototype = new Animal(); – linstantnoodles

1
function Animal(name , year){ 
    alert(name); 
    this.name = name; 
    this.year=year; 

    this.age= function(){ 
     var n= new Date().getFullYear(); 
     return n-this.year; 
    }; 
} 
    function Dog(name , year , color , type) {  
     this.color=color; 
     this.type=type; 
     Animal.call(this, name,year); 

     //override method age of animal 
     this.age= function(){ 
      var n= new Date().getFullYear(); 
      return (n-this.year)*7; 
     }; 

     Dog.prototype.age(); 
    } 

// you invoke Animal as constructor. but you did not pass any argument to it . so name any year will be undefined in Animal() so alert will show undefined 
    Dog.prototype = new Animal(); 

// here is the normal invoke so alert will show rex 
var p1 = new Animal("rex",2008); 

代碼將調用動物兩次,第一次調用警報未定義和第二警報雷克斯

0

Dog.prototype =新動物();

您正在執行不帶參數的Animal()。它可以模擬JavaScript中的繼承。