2013-01-18 42 views
0

哪种方法可以从JavaScript中的返回对象引用私有方法?我离开你一些示例代码:如何从javascript中的返回对象引用私有方法?

var HelloWorld = (function(){ 
    var hello = function(){ 
     console.log("hello") 
    }, 
    return{ 
     addToList: function(){ 
      //how can i reference hello from here dynamically like: 
          //this["hello"] or this["hell" + "o"] 
     }  
    } 
})() 
+1

你有没有尝试过?发生了什么? ''['hello']();'对我来说似乎很好...... – 11684

+0

顺便说一句,你有一个语法错误,你应该在'hello'函数声明后面加一个分号,而不是逗号。 – 11684

+2

不知道你是否也遇到过这个问题,但是在返回之前/之后的hello函数应该是';'。 – ericponto

回答

1

hello()不是一种方法。这只是封闭中的一个函数。所以,你可以从你的addToList()方法中调用它。

如果您希望hello函数的行为与this设置为对象实例的方法类似,您必须将该实例传递给它,如本例中所示。

var HelloWorld = (function(){ 
    var hello = function(){ 
     console.log("hello") 
    }, 
    return{ 
     addToList: function(){ 
      //how can i reference hello from here dynamically like: 
      hello.call(this); 
     }  
    } 
})() 

如果你真正想做的是一个字符串引用来访问Hello功能,你不能轻易通过字符串名称访问本地变量。如果你想做到这一点,你可以有把Hello功能到本地对象是这样的:

var HelloWorld = (function(){ 
    var locals = { 
     hello: function(){ 
      console.log("hello") 
     } 
    }; 
    return{ 
     addToList: function(){ 
      //how can i reference hello from here dynamically like: 
      locals["hello"].call(this); 
     }  
    } 
})() 
+0

当你发布一个答案的同时,当你发布一个编辑时,给你一个+1,并且我们得出了同样的结论。 – JAAulde

+0

@JAAulde - 我会说OP的问题不是很清楚,所以我提供了几个不同的可能答案。 – jfriend00

2

因为返回仍然是瓶盖内,你可以直接调用hello。所以:

hello(); 

为了使其“动态的”,因为其他答案建议,你需要hello保存的东西。如果您想将其附加到this而不是另一个对象,则只需存储对this的引用,以便稍后可以访问它。

var HelloWorld = (function(){ 
    var self = this; 
    this.hello = function(){ 
    console.log("hello") 
    }; 
    return { 
    addToList: function(){ 
     self["hello"](); 
    }  
    } 
})(); 
+0

这是正确的,除了我在OP的示例代码注释中读到的内容。它提到请求“动态”访问,我假设这意味着给出一个字符串并用于引用该函数。 – JAAulde

+0

啊,好点。这确实使事情变得复杂一点。 – ericponto

1

由于是,你不能引用函数hello“动态”中的示例代码注释中。 (我假设你的“动态”定义将被赋予一个包含单词“hello”的字符串,并以某种方式使用它来引用函数hello。)

但是,您可以将hello移动到对象中并引用它关闭使用方括号是对象:

var HelloWorld = (function() { 
    var privateCode = { 
     hello: function() { 
      console.log('hello'); 
     } 
    }; 

    return { 
     addToList: function() { 
      // access via `privateCode['hello']()` 
     }  
    }; 
}()); 
1

不能使用this,因为你还没有它的对象的一部分。

var HelloWorld = (function() { 

    var hello = function() { console.log("hello"); }; 

    return { 
     addToList : function() { hello(); } 
    }; 

}()); 

这将工作正常。

如果您需要使用字符串来访问它,那么你需要做hello,那么你有两个选择:

1)做出公开功能,您可以用字符串,它调用hello调用

return { 
    command : function (string) { if (this[string]) { this[string](); }, 
    sayHello : function() { hello(); } 
}; 

2)拨打私人对象,它存储你的方法(那么你可以用绳子把它):

var private_actions = { 
    hello : function() { console.log("hello"); } 
}; 

return { 
    command : function (string) { 
     if (private_actions[string]) { private_actions[string](); } 
    } 
}; 
1

由于hello只存在于一个匿名IIFE的范围,你需要将它存储在为了一些中间对象,以便能够从公共方法动态访问hello

var HelloWorld = (function(){ 
    var privates = { 
     hello: function(){ 
      console.log("hello") 
     } 
    }; 

    return { 
     addToList: function(){ 
      privates['hello'](); 
     } 
    } 
})(); 

HelloWorld.addToList(); 

Working Example

相关问题