I have object like this:
var example = {
test1: 10,
test2: 20,
test3: 30
}
For some reason, I wanted to use test# variables without example, so I extended window object like this(using jQuery):
$.extend(window, window, example)
But, whenever this function has called, page was refreshed.
And one more question, if I try to extend document in the same way, some error occurs like this:(in Chrome)
Error: NAMESPACE_ERR: DOM Exception 14
Why is that? And is there any way to use variables without object(namespace) name?
Your
$.extendcall does the equivalent of this:And that will refresh the page.
The
$.extendfunction essentially copies all the properties (using simple assignments) from the second through last arguments to the first argument and returns the (updated) first argument. In your second argument,window, there will belocationproperty and when you assign something towindow.location, you change the page and if the new and oldlocationvalues are the same, you’ll refresh the current page.If you want to add all the properties in
exampletowindow, just do this:As far as “And is there any way to use variables without object(namespace) name?” goes, just add your variables as properties of
windowand you’ll be able to access them without a prefix:For example: http://jsfiddle.net/ambiguous/T7cNx/