简体   繁体   English

控制台访问$(document).ready函数本地的Javascript变量

[英]Console access to Javascript variables local to the $(document).ready function

How can I access some variables inside 我怎样才能访问里面的一些变量

$(document).ready(function(){
    var foo=0;
    var bar = 3;
});

from Google chrome console? 来自谷歌Chrome控制台? If I try alert(foo), I will get a message that it is not defined. 如果我尝试alert(foo),我会收到一条消息,说明它没有定义。

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. 不要仅因为您不知道如何使用正确的工具而使用不良做法。

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;
});

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)

You can't since the are in a closure space. 你不能因为它们处于封闭空间。 Here it explains how closure works ( How do JavaScript closures work? ). 这里解释了闭包是如何工作的( JavaScript闭包如何工作? )。 To access the varible just set a breakpoint inside the $(document).ready function 要访问varible,只需在$(document).ready函数内设置一个断点

If you really need 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;
});

Note that they will only be set once the document is loaded! 请注意 ,只有在加载文档后才能设置它们! Therefore alert(myObj.foo); 因此alert(myObj.foo); (or something similar) place immediately after the $(function() { ... }); (或类似的东西)紧跟在$(function() { ... }); block will yield undefined ! 块将产生undefined

If you only need to access them inside that context, then do not declare anything outside the function. 如果你只需要访问他们上下文中,那么申报的功能以外的任何东西。 And try to debug your code with other methods. 并尝试使用其他方法调试您的代码。 With chrome, console.log is quite helpful, for instance. 例如,使用chrome, console.log非常有用。

$(document).ready(function(){
    window.foo=0;
    window.bar = 3;
});

You expose those vars into global scope(really not advised) 您将这些变量暴露在全局范围内(真的不建议)

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);
});

Actually there is a hackish way around it. 实际上,围绕它有一种hackish方式。 You should probably not use it. 你可能不应该使用它。 Set a Breakpoint. 设置断点。 That being said, here's the code: 话虽这么说,这是代码:

$(document).ready(
    readyClosure = function(access){
        var x = 5; 
        return eval(access);
    }
);
readyClosure('x'); // returns 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM