控制台访问 $(document).ready 函数本地的 Javascript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13766371/
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
Console access to Javascript variables local to the $(document).ready function
提问by Mircea Voivod
How can I access some variables inside
我如何访问里面的一些变量
$(document).ready(function(){
var foo=0;
var bar = 3;
});
from Google chrome console? If I try alert(foo), I will get a message that it is not defined.
从谷歌浏览器控制台?如果我尝试 alert(foo),我会收到一条消息,指出它未定义。
回答by Florian Margaine
Put a breakpoint with the debugger. You'll get full access to them when the debugger will stop.
用调试器放置一个断点。当调试器停止时,您将获得对它们的完全访问权限。
Other answers telling you to put them in the global scope are bad. Don't use bad practices just because you don't know how to use the right tools.
其他告诉您将它们放在全局范围内的答案很糟糕。不要仅仅因为您不知道如何使用正确的工具而使用不好的做法。
回答by Xophmeister
You can't access these variables because they are defined within a functional closure. The only way you could do it is if you made a global reference to them outside your function's scope.
您无法访问这些变量,因为它们是在函数闭包中定义的。你能做到的唯一方法是在你的函数范围之外对它们进行全局引用。
var foo, bar;
$(document).ready(function(){
foo = 0;
bar = 3;
});
回答by Eric Herlitz
Why not do a proper expose variable ?
为什么不做一个适当的暴露变量?
$(document).ready(function(){
var foo=0;
var bar = 3;
$.exposed = {
foo: foo,
bar: bar
}
});
Check your variables by doing
通过执行检查您的变量
console.log($.exposed.bar)
回答by Stefan
You can't since the are in a closure space. Hereit explains how closure works (How do JavaScript closures work?).
To access the varible just set a breakpoint inside the $(document).readyfunction
你不能,因为它们在封闭空间中。这里解释了闭包是如何工作的(JavaScript 闭包是如何工作的?)。要访问变量只需在$(document).ready函数内设置一个断点
回答by Yanick Rochon
If you reallyneed to access these variables from different parts of your code (initialize them on document ready, then accessing them elsewhere, for example), then you have to declare them outside the function closure.
如果您真的需要从代码的不同部分访问这些变量(例如,在文档准备好时初始化它们,然后在其他地方访问它们),那么您必须在函数闭包之外声明它们。
If and only if this is the case, I'm not a fan of cluttering the global space. I would suggest you to use a base object for that :
当且仅当是这种情况时,我不喜欢弄乱全球空间。我建议您为此使用基础对象:
var myObj = {};
$(function() {
myObj.foo = 0;
myObj.bar = 3;
});
Notethat they will only be set once the document is loaded! Therefore alert(myObj.foo);(or something similar) place immediately after the $(function() { ... });block will yield undefined!
请注意,只有在加载文档后才会设置它们!因此alert(myObj.foo);(或类似的东西)在$(function() { ... });块之后立即放置会产生undefined!
If you only need to access them inside that context, then do notdeclare anything outside the function. And try to debug your code with other methods. With chrome, console.logis quite helpful, for instance.
如果你只需要访问他们上下文中,那么就不能声明函数以外的任何东西。并尝试使用其他方法调试您的代码。例如,使用 chromeconsole.log非常有帮助。
回答by trias
Actually there is a hackish way around it. You should probably not use it. Set a Breakpoint. That being said, here's the code:
实际上有一个hackish方法。你可能不应该使用它。设置断点。话虽如此,这是代码:
$(document).ready(
readyClosure = function(access){
var x = 5;
return eval(access);
}
);
readyClosure('x'); // returns 5
回答by Vytautas Butkus
$(document).ready(function(){
window.foo=0;
window.bar = 3;
});
You expose those vars into global scope(really not advised)
你将这些变量暴露在全局范围内(真的不建议)
回答by jtheman
define them outside of $(document).ready() and then access them inside as well.
在 $(document).ready() 之外定义它们,然后也在内部访问它们。
var foo = 0, bar = 3;
$(document).ready(function(){
alert(foo);
});

