admin管理员组文章数量:1331091
I'm using RequireJS in my browser. I have some JS loaded by a script tag, something like:
requirejs(["jquery", "shared", function($, shared) {
var foo='bar';
}
I would like to print the current value of 'foo' from a JS console in the Chrome Dev Tools. How can I do this?
Edit 2: This question was originally very vaguely worded - sorry about that. To clarify, I do not wish to stop using RequireJS, pollute global, or know in advance what it is I want to debug.
I'm using RequireJS in my browser. I have some JS loaded by a script tag, something like:
requirejs(["jquery", "shared", function($, shared) {
var foo='bar';
}
I would like to print the current value of 'foo' from a JS console in the Chrome Dev Tools. How can I do this?
Edit 2: This question was originally very vaguely worded - sorry about that. To clarify, I do not wish to stop using RequireJS, pollute global, or know in advance what it is I want to debug.
Share edited May 14, 2013 at 14:31 mikemaccana asked Jul 31, 2012 at 8:16 mikemaccanamikemaccana 124k110 gold badges430 silver badges533 bronze badges5 Answers
Reset to default 4Set a breakpoint in Chrome or Firebug to break at the point of your closure. Foo will then be available to the console until you resume script execution.
edit: Scope will still matter. If the variable is private within a member of shared, you'll need to set a break in shared.js instead, e.g. if we assume shared.js contains:
var shared = {
myFunc: function() {
var foo = 'bar';
// break here
}
}
If I understand the question, the whole point of using RequireJS is not to use global variables!
Therefore I define a console.js
module like this:
define(function() {
var nativeConsole = {};
try {
// for IE 8/9
if (!window.console) {
window.console = {
log: function() {},
debug: function() {},
info: function() {},
warn: function() {},
error: function() {}
};
}
nativeConsole = console;
// Firefox throws an exception if you access the console object and it doesn't exist. Wow!
} catch (e) { }
return nativeConsole;
});
Then use it like this:
requirejs(["jquery", "console", function($, console) {
var foo='bar';
console.warn("foo = " + foo);
}
you can do that with
`requirejs(["jquery", "shared", function($, shared) {
var foo=bar;
console.debug(foo);
}`
The same mand works in Firefox too.
The JS Console already has the context of your page. Just open the console, type "foo", then press Enter.
Move the var definition outside of the closure.
var foo;
requirejs(["jquery", "shared", function($, shared) {
foo='bar';
});
The downside to this is that foo
now exists in the global window
scope (the variable exists as window.foo
), and global variables should be avoided. It can be helpful for debugging though. Alternatively, just define it to window
as such:
requirejs(["jquery", "shared", function($, shared) {
window.foo='bar';
});
Or, if you plan to do this and keep the variable around past the debugging stage, you should consider creating a custom namespace for "your" variables.
var myNamespace = {};
requirejs(["jquery", "shared", function($, shared) {
myNamespace.foo='bar';
});
And then you can window.console && console.log(myNamespace.foo)
.
版权声明:本文标题:javascript - RequireJS in the browser: how can I log a variable without modifying my code first? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265649a2443339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论