2010-10-17 29 views
0

最近我一直在研究Javascript原型,可以理解理論,並相信它對我的語言理解非常有用,但不能完全得到以下的工作...Javascript Prototyping Error

var player = function(){//Declarations here}; 
var super_player.prototype = new player(); 

每一個編譯器/檢查器在第2行標記'缺少分號'錯誤。 我很難過,但相信我忽視了一些非常簡單的事情。

有人能指出我正確的方向嗎?

+3

是所使用的確切的代碼嗎?評論正在創建一個語法錯誤。 – PleaseStand 2010-10-17 21:11:16

回答

0

你想說

super_player.prototype.player = function(){ /*Declarations here*/ }; 

2

引擎不知道super_playerfunction,直到您聲明爲一個,因此它沒有prototype

var player = function() {}, 
    super_player = function() {} 

// now we can happily set the prototype :) 
super_player.prototype = new player(); 

// don't forget to point the constructor back to super_player 
// not doing so will cause great confusion 
super_player.prototype.constructor = super_player 
2

您對第1行的評論阻止了結束大括號。所以你有一個搖晃的開放式支架,它不起作用。你可以將它更改爲以下:

var player = function() { 
    // Declarations here 
}; 

如果我是你,我覺得你要考慮使用駝峯類名執行以下操作:

function SuperPlayer() { 
} 

function Player() { 
} 
Player.prototype = new SuperPlayer(); 
Player.prototype.constructor = SuperPlayer; 

這將使SuperPlayer的基類,和Player作爲Player從SuperPlayer通過其原型繼承而派生的類。而不是像你在上面的例子中那樣。

+0

我將不得不更詳細地研究你的答案。更重要的是,感謝您閱讀我的內聯評論生成的語法錯誤。這不是我工作的例子,但是當我在這裏發表時,我應該更清楚!^_^ – Charlie 2010-10-17 21:40:31

3

你想要做這樣的事情

function Player() { 
    // player things here 

} 

Player.prototype = new SuperPlayer(); // get all the things on SuperPlayer prototype 
Player.prototype.constructor = Player; 

假設SuperPlayer是超一流的球員,因爲它是。

編輯 - 如果SuperPlayer是一個更好的球員,也就是玩家的一個子類,只是扭轉上述格局

function SuperPlayer() { 
     // super player things here 

    } 

    SuperPlayer.prototype = new Player(); // get all the things on Player prototype 
    SuperPlayer.prototype.constructor = SuperPlayer; // the above line changed the  constructor; change it back 

我分不清你寫什麼,如果SuperPlayer是一個子類,或者沒有。此外,其他答案指出,您發佈的代碼在語法上由於評論而被破壞...

+0

其他方式實際上。 :}我一直在思考'超級',暗指增強,而不是基礎。抱歉! – Charlie 2010-10-17 21:35:43

0

正在查看chubbards的答案...原型關鍵字可以被最好地描述爲笨拙..這很奇怪,因爲它確實是面向對象的,但實現是瘋狂的!

舉一個常見的例子...

mammal() = object; 
cat() = prototype; // Implements mammal(); 
tiger() = object; // Implements cat(), but does NOT require the 'prototype' keyword... like some kind of 'cystalline' form? I can then create a whole range of individual 'tigers', but will not be able to add any new properties to any of them... 

這個怎麼樣,而不是?:

machine() = object; 
car() = prototype; // Implements machine. It's a machine afterall... 
super_car = prototype; // It's a very fast car... 
ferrari = prototype; // They only make supercars... 
ferrari_355 = object; // One of the best cars they've ever made. No need to change it, right? :} 

是正確的? :|

+0

你寫的只是一個結構,而不是Javascript。如果你發現原型繼承概念很奇怪(就像很多人一樣),那麼你可以使用其他的Javascript庫,比如ExtJS或prototype,它們提供了以更自然的方式聲明對象層次結構的方法。 Prototype是Function的一個屬性。它不能單獨使用。所以你必須有:cat.prototype = new mammal(),或者tiger.prototype = new cat()。有了像Ext.JS之類的東西,你可以做像cat.extends(哺乳動物)這樣的事情。在封面下它大致上是一樣的。 – chubbsondubs 2010-10-18 14:19:09

+0

由於我們只有600個字符,因此很難迴應。我會盡量簡短。 Javascript使用函數來定義不僅規則的函數,而且定義像類的概念。以大寫字母開頭的函數表示作爲構造函數的函數。當我們寫'新的SomeObject()'。 new關鍵字分配一個空對象,將this關鍵字設置爲該新對象,然後調用SomeObject函數。然後,SomeObject可以使用this.someMember語法爲數據成員提供該對象。因此,使SomeObject像一個構造函數... – chubbsondubs 2010-10-18 14:29:04

+0

現在原型是每個函數都有的屬性。該屬性形成了一個財產分辨率發生的鏈。當一個屬性在一個對象上被引用時,它首先在該對象的內部尋找一個屬性,如果該屬性不存在,那麼它將查找具有相同名稱的屬性的原型,並繼續遵循原型直到它找到它。它模仿同樣的OO語言如何工作來解析繼承層次結構中的成員。我們創建一個新的SomeObject()的原因是,在該構造函數中創建的所有實例變量都將被初始化。 – chubbsondubs 2010-10-18 16:47:28

0

非常尊重你Chubbard。 :)

你的例子幫了很大忙。

我原來的'例子'是相當輕浮。 :P

我還在跑成問題了一把,雖然... 要闡述我的「貓」樣品(保持事物儘可能簡短):

function mammal(){ 
// 'mammals' constructor - My 'empirical' 'class'... 
} 
    mammal.count = 0; // To keep track of the zoo I've created. No subclass can access this property. It's a 'static' property 

    // I could have declared this 'method' inside of my constructor 
    // with an anonymous function attached to (this.prototype.breathe). 

    mammal.track = function(){ 
     this.count++; 
    } 

    mammal.prototype.breathe = function(){ 
     alert("Inhale... Exhale..."); 
    } 

function cat(){ 
// 'cat' constructor 
} 

// All cats shall now become a type of mammal 
cat.prototype = new mammal(); 

cat.prototype = function(){ 
// This anonymous function is the REAL constructor for my 'cat' 'superclass' 
} 

    cat.prototype.meow = function(){ 
     alert("Meow!"); 
    } 

function lion(){ 
// The king of jungle... 
// I can keep track of the number of 'mammal' instances I create here 
mammal.track(); 
} 

// All lions are cats, afterall... 
lion.prototype = new cat(); 
// Also note that I have no plans to extend the lion class. 
// I have no need of a class below the 'idea' of a lion 

    lion.name = "Kitty"; // :} 

    // Here's where I get confused... 
    // I can set (lion.name) via instances, can't call (lion.pounce), but (lion.prototype.roar) works all day long! o_0 
    lion.pounce = function(){ 
     alert(this.name+" pounces...") 
    } 

    lion.prototype.roar = function(){ 
     alert(this.name+" doesn't meow, he ROOOAAARS!"); 
    } 

// With these constructs in place, I can now script... 
$(document).ready(function(){ 

     var rory = new lion();  
     var napoleon = new lion(); 

     alert("We have "+mammal.count+" mammals running about"); 

      // This is 'Rory'... 
      rory.name = 'Rory'; // Respect the pun... 
      rory.roar(); 

      // This is 'Napoleon'... 
      napoleon.name = 'Napoleon'; 
      napoleon.breathe(); // Napoleon can't breathe... he didn't inherit mammal.prototype.breathe(), for some reason 
      napoleon.roar(); // How am I able to set (lion.name), but not call lion.pounce()? 
      napoleon.pounce(); 

}); 

你是當然的權利,我鏈中的每個類都直接創建最終實例,是一個原型函數。但爲什麼(lion.name)工作,但沒有(lion.prototype.name)。相反,爲什麼lion.prototype.pounce()可以工作,而lion.pounce()是borks?

拿破崙和羅裏都是獅子畢竟...

我有負載更多的JavaScript的問題......這是一個非常奇怪的語言......;)