2012-07-04 35 views
0
var foo = { 
    p1: function(){ 
    return this.p2; 
    }, 

    p2: function(){ 
    console.log('i am foo.p2'); 
    } 
}; 

我試圖做類似上面的例子中的東西,但是我運行到哪裏,當我調用一個問題:的Javascript是有關功能的詳細信息,對象的屬性「這個」

var result = foo.p1(); 

結果=='undefined'

我對'this'在對象上下文中的工作方式感到困惑。有人能解釋我哪裏錯了嗎?

編輯 更完整的例子:

suite_segments.themis = { 

    //don't re-run themis initialization script 
    initialized: false, 

    /** 
    * Initializer for themis product. Returns true if initialization 
    * operations were performed, false if not (most likely because 
    * the product was already initialized -- not a fresh navigation) 
    */ 
    init: function(){ 

      //prevent multiple initializations 
      if(this.initialized) 
       return false; //did not initialize 
      this.initialized = true; 

      //operations 
      jQuery('#tabs').tabs(); 


      //init success 
      return this.themis_destroy;   
    }, 





    /* ---------------------------------------------------------------------------------- 
    *  DESTRUCTORS 
    * ----------------------------------------------------------------------------------/ 
    /** 
    * Function to be invoked if user navigates away from 'themis' entirely. Other 
    * sub-destroy type functions will be invoked if necessary when a user switches 
    * between parts of themis 
    * 
    */ 
    themis_destroy: function(){ 

     console.log('themis_destructor'); 
     this.initialized = false; 

    }, 
    /** 
    * Designed to be overwritten every time a segment of themis is loaded. Will be invoked 
    * ever time a segment of themis is loaded. 
    */ 
    themis_sub_destroy: function(){} 


}; 
+0

發佈您正在嘗試或製作[fiddle]的實際代碼(http://jsfiddle.net) – zzzzBov

+2

'foo.p1()'確實會產生'foo.p2'。發佈更完整的摘要。 – MaxArt

+0

我不知道,男人,你的代碼段實際上在JSFiddle中有效。 'suite_segments.themis.init();'真的返回'suite_segments.themis.themis_destroy'。必須有別的東西。 – MaxArt

回答

1

你完成的例子也會起作用。使用您的代碼,suite_segments.themis.init()返回描繪函數(或false),而不是undefined

但你有其他問題:析構函數將無法正常工作。閱讀this excellent overview about the this keyword,你會看到:this指向當前的上下文,這是依賴於調用。當根據...themis.init()調用時,該函數將在themis對象的上下文中調用 - 一切正常。但返回的函數(suite_segments.themis.destroy)不會在對象上調用,但(我猜)是獨立的 - 並且沒有機會設置正確對象的initialized屬性。

在你的情況,我可以recommmend的.bind() method設置返回的功能的情況下:

return this.themis_destroy.bind(this); 

又見this blog post about "objects with properties that are functions" or the mythof of methods,這正好覆蓋你的標題問題,this post about this

1

道格·克羅克福德有物體內具有良好的page about private/public/privileged members,以及有關定義這些成員的最佳實踐會談。這可能有助於清除圍繞this變量的一些混淆。

但是,在您的示例中,您不能向我們展示某些東西。的foo.p1返回值是函數foo.f2如下所示:

http://jsfiddle.net/erSWu/1/

+0

雖然通過使用構造函數給對象私有的「成員」可以解決OP的問題,但該文章完全沒有用處,沒有進一步的解釋,特別是關於'this'關鍵字(文章不*澄清) – Bergi

0

你正在做一個礦工兄弟的錯誤...

它應該是這樣的:

var foo = { 
    p1: function(){ 
    return this.p2(); 
    }, 

    p2: function(){ 
    console.log('i am foo.p2'); 
    } 
}; 
+1

不,*那*會返回'undefined',因爲'foo.p2'不會返回任何東西。 – MaxArt

+0

但它會調用該函數並在控制檯上記錄文本... –

+0

這不是凱西想要的。他希望返回*參考*到'foo.p2',因爲在他更詳細的代碼片段中'foo.p2'是一個將在後面調用的析構函數。 – MaxArt