Sometimes you may have a library that modifies window.location, and you want it to function normally, but also be tested. If so, you can use closure to pass the desired link to your library, for example.
(function(view){ view.location.href = "foo"; }(self || window));
Then in your test, before you include your library, you can redefine yourself globally and the library will use mock self as a view.
var self = { location: { href: location.href } };
In your library, you can also do something like the following so that you can redefine yourself at any time during the test:
var mylib = (function(href) { function go ( href ) { var view = self || window; view.location.href = href; } return {go: go} }());
In most, if not all modern browsers, I already refer to the window by default. On platforms that implement the Worker API, inside the Worker itself. It is a reference to the global area. In node.js, both I and the window are not defined, so if you want, you can also do this:
self || window || global
This may change if node.js really implements the Worker API.
Jim Isaacs Oct. 15 '11 at 7:14 2011-10-15 19:14
source share