JavaScript:从外部访问匿名函数内部的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14373475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: access variables inside anonymous function from the outside
提问by steve
Say I have this anonymous function:
假设我有这个匿名函数:
(function(window){
var private = 'private msg';
function sayit() {
alert(private) // works
}
document.body.onclick = sayit; // works
})(window);
// private shouldn't be accessible here
Is this how JavaScript should behave?
这是 JavaScript 的行为方式吗?
That is, there is no way to access privatefrom anywhere outside of that anonymous function?
也就是说,没有办法private从匿名函数之外的任何地方访问?
If so, is it possible to find some kind of hack to access privatefrom the outside, leaving the code the way it is?
如果是这样,是否有可能找到某种private从外部访问的黑客,让代码保持原样?
采纳答案by Faiz
Yes, this is how Javascript lets you have 'private' variables (hidden in a function scope).
是的,这就是 Javascript 让您拥有“私有”变量(隐藏在函数作用域中)的方式。
No, there's no hack available to access variables such as privatewithout re-writing the code.
不,没有 hack 可用于访问变量,例如private无需重新编写代码。
Variables defined with varwithin a function can be accessed only from within that function.
var在函数内定义的变量只能从该函数内访问。
回答by SangYeob Bono Yu
Ok. I got it.
好的。我知道了。
(function(window){
var alert_original = window.alert;
window.alert = function(data) {
window.extracted = data;
alert_original(data);
};
})(window);
(function(window){
var private = 'private msg';
function sayit() {
alert(private) // works
}
document.body.onclick = sayit; // works
})(window);
After you click body, you can get 'private msg' from extracted
点击正文后,您可以从 extracted
回答by Eevee
They aren't intended as "private" variables; that's just how closures work. You can do the same thing in Perl and Python, at the very least, and probably a great many other languages with closures and lexical scoping.
它们不是作为“私有”变量;这就是闭包的工作方式。您至少可以在 Perl 和 Python 中做同样的事情,而且可能还有很多其他具有闭包和词法范围的语言。
Debuggers like Firebug or Chrome Inspector can still show you the entire stack at any point (including closed-over variables), but other than that and without changing the original code, I think you're out of luck.
像 Firebug 或 Chrome Inspector 这样的调试器仍然可以在任何时候向您显示整个堆栈(包括封闭变量),但除此之外并且不更改原始代码,我认为您不走运。
Perhaps if you told us your actual problem... :)
也许如果你告诉我们你的实际问题...... :)
回答by gurvinder372
That's the whole point of having scope and private variables
这就是拥有作用域和私有变量的全部意义
Either
任何一个
Set the private value to a global variable?
将私有值设置为全局变量?
or
或者
declare the variable outside
在外面声明变量
回答by petermk
You would have to do something like this:
你必须做这样的事情:
var Test = (function(window){
var private = 'private msg';
var api = {};
function sayit(){
alert(private) // works
}
document.body.onclick = sayit; // works
api.sayit = sayit;
return api;
})(window);
Test.sayit(); //this would alert 'private msg'

