Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why attach to window [edited]

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?

like image 284
Gutzofter Avatar asked Jan 23 '23 09:01

Gutzofter


1 Answers

All 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 complex 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-

like image 71
Nick Craver Avatar answered Feb 01 '23 05:02

Nick Craver