控制台日志中的奇怪变量未定义错误 [英] Strange variable undefined error in console.log

查看:292
本文介绍了控制台日志中的奇怪变量未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么控制台以这种方式工作吗?



我发布了这个问题一次,但我不能正确配置,所以我再次关闭。最初的问题没有提到问题可能是控制台相关的,我只是质疑变量的奇怪行为。我基本上只贴了以下截图:



上记录 $ s.page



重新打开问题

$ s.page.navi b
$ b

没有人能给出答案,虽然 @zerkms 建议它可以与控制台本身有关。在我关闭问题后,他通过此帖子与我联系:有require.js加载我的文件,只有当我真的需要他们可能重新打开问题。



问题



足够介绍,回到问题。那么变量在哪里。从其他问题我发布你可以看到我使用require.js加载我的文件。



$ s.page的代码我做以下(简化):

  require(['nav'],$ .proxy 

this.navi = new Nav.Views.Main();

},this));

nav 必须加载。



让我更详细地登录:

  console.log('x'); 

require(['nav'],$ .proxy(function(Nav){

console.log('y');

this.navi = new Nav.Views.Main();

},this));

我把其他日志留在aswel,这里是输出:



>



注意,首先记录 x 。当我然后记录 $ s.page 它显示了 $ s.page 未来,我想。真的很奇怪。



当我然后在下一行记录 $ s.page.navi 这是正确的。



然后在最后,甚至不再被调用, y 加入党。 / p>

解决方案

参见router.js的第22行,我调用 $ s.page.navi.start ,这是这个问题的整个问题。它不能被解雇。我用下面的代码解决它:

  require(['nav'],function(Nav){

$ s.page.navi.start();

});

它只是等待它的依赖。



< h2>结束语

当使用JavaScript,以及如backbone.js和require.js的库时,你应该真正记住你在做什么,并记住,脚本不容易阻止。



只需在应用程序的某些点进行日志记录并不总是帮助你解决你的问题,你必须从应用程序的根目录叶子的尖端看到实际发生了什么。最好的方法是在调试器中构建。


Can someone explain why the console acts this way?

I posted this question once, but I could not quite formulate it correctly so I closed it again. The initial question did not mention that the problem could be console related, I just questioned the strange behavior of the variables. I basically only posted the following screenshot:

Note here that I log $s.page on line 20 and $s.page.navi is there, and on line 21 when I log again it's gone.

Reopening the question

There was nobody who could give an answer, although @zerkms suggested it could have something to do with the console itself. After I closed the question he contacted me via this post: Have require.js load my files only when I actually need them to maybe reopen the question.

The Problem

Enough intro, back to the issue. So where did the variable go. From the other question I posted you can see I use require.js to load my files. I am kinda new to this all so my problem was related to it.

In the code of $s.page I do the following (simplified):

require(['nav'], $.proxy(function(Nav){

    this.navi = new Nav.Views.Main();

}, this));

nav is a module that require.js has to load. I the script would block until the file is there, and looking at the console it confirmed that.

Let me log in more detail:

console.log('x');

require(['nav'], $.proxy(function(Nav){

    console.log('y');

    this.navi = new Nav.Views.Main();

}, this));

I left the other logs in place aswel, and here is the output:

Notice that x is logged first. When I then log $s.page it shows me what $s.page will look like in the future, i guess. Really odd.

When I then log $s.page.navi on the next line it is not there, which is correct.

Then at the very end, without even being called anymore, y joins the party.

解决方案

See on line 22 of router.js I call $s.page.navi.start(), that was the whole point of this problem. It could not be fired. I solved it with the following code:

require(['nav'], function(Nav){

    $s.page.navi.start();

});

It will just wait for it's dependencies.

Conclusion

When working with JavaScript, and libraries such as backbone.js and require.js you should really keep track of what you are doing and keep in mind that the script does not easily block.

Just logging at certain points in the application will not always help you solve your problem, you have to really log from the root of your application to the tip of the leaf to see what is actually happening. And the best approach is always the build in debugger.

这篇关于控制台日志中的奇怪变量未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆