更新:刚刚整理这篇文章,因为问题似乎略有改变。在断点上暂停时, Chrome 开发工具似乎无法正常工作。
编辑:更新了第一个示例,它似乎是chromes调试工具的问题。看来如果你在otherFunc
中没有包含console.log语句并在调试器上暂停,那么变量是未定义的。它们应该在Firefox中正常工作。
var a = (function(window, document, undefined) {
var View = {
initialize: function() {
// this prints
console.log('started the view');
},
};
var Controller = {
start: function() {
View.initialize();
this.otherFunc();
},
otherFunc: function() {
// try logging View and Controller in console
debugger;
// View and Controller are undefined
// using console.log works eg console.log(View) does not give error.
},
}
Controller.start();
})(window, document);
与以下相反,如果您包含控制台日志语句,则可以从chromes dev工具访问该变量。
var a = (function(window, document, undefined) {
var View = {
initialize: function() {
// this prints
console.log('started the view');
},
};
var Controller = {
start: function() {
View.initialize();
this.otherFunc();
},
otherFunc: function() {
// We can now access View from the console while paused.
debugger;
console.log(View); // seems to fix the issue
},
}
Controller.start();
})(window, document);