admin管理员组文章数量:1356071
I was looking over the code for qunit.
My question is why would you want to attach the qunit object via property to window object.
Here is the link to the file. Look at line 11.
If I look at a unit test run using firebug you can see it is a property of window.
[edit] Additional: Is there a specific reference for best practice for declaring things in specific namespaces?
I was looking over the code for qunit.
My question is why would you want to attach the qunit object via property to window object.
Here is the link to the file. Look at line 11.
If I look at a unit test run using firebug you can see it is a property of window.
[edit] Additional: Is there a specific reference for best practice for declaring things in specific namespaces?
Share Improve this question edited Mar 24, 2010 at 17:18 Gutzofter asked Mar 24, 2010 at 2:10 GutzofterGutzofter 2,02323 silver badges26 bronze badges2 Answers
Reset to default 7All global objects (functions, variables, etc) are just children of window, it's the default context.
For example: window.jQuery
or window.$
It may be easier to think of it this way...where else would you put them? When you're doing something this general, best (or at least easiest) to stick them in the default place. If you're doing something plex with lots of functions, objects, etc...best to put them in a namespace or within an object. For example all of jQuery's code is under jQuery
, not littered in the root of the DOM like window.ajax
, instead it's jQuery.ajax
.
This is much neater, but perhaps overkill when you're dealing with a few items, but it's a good idea to make sure they are unique if this is the case...which qunit does, by prefixing their objects with qunit-
Attaching globals as properties of window
is bad practice. All globals should be declared using var
. Here's my reasons:
- It makes static analysis of source code much harder. It is impossible to tell from looking at a script which globals will be declared and when. Undeclared globals will create
ReferenceError
s if they're used. Usingvar
means JavaScript's hoisting takes effect, and mitigates this problem. - Globals made this way are fundamentally different, and there is no easy way for your code to detect this. The biggest difference is the absence of
[[DontDelete]]
on globals made this way, which means you can delete your global variables. This is silly. - It will tempt you to declare your globals from outside the global scope. This is magic, and bad magic at that. Don't do it.
As far as I'm concerned, the fact that window.x = 1
creates a global variable named x
is an interesting curiosity of JavaScript, but should not be used nor replied upon. There are, however, good reasons to use properties of window
, since it's an object like any other (more or less). In these cases, you should use the full name, e.g. window.onload
instead of just onload
.
本文标签: javascriptwhy attach to window editedStack Overflow
版权声明:本文标题:javascript - why attach to window [edited] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743950958a2567279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论