2012-07-04 47 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