开发者

How do I access javascript functions in the "object Window"

I feel silly asking this in such a crude way, but in Firebug I see my current object开发者_如何学C (this) and below that a line then, "object Window," where the global namespaces seem to live (e.g., there's a $ object, jQuery object ...). I have an object I created called "g" with a function called "Update" that lives in this global namespace (again, using that term loosely).

Am I using the right terminology here? Does the jQuery object live in the "global namespace"? Furthermore, if I want to call something living here, do I just need to call, say, g.Update()? Would it be right to say these are the equivalent of what you'd call static variables in OO terminology?

Thanks!


If i understand the question correctly, default last scope is 'window', so if you didn't declare 'g' anywhere else in your scope, the last place it will look in is 'window'

Working example

g = { Update: function() {} };

function foo()
{
    g.Update();
}

Not working example:

g = { Update: function() {} };

function foo(g)
{
    g.Update();
}


You're kind of using the right terminology, when someone refers to the "global scope", they mean window...it's the top-most scope there is.

For jQuery: yes, jQuery lines in the global namespace, window.jQuery or the alias window.$.

How you call something depends on your structure, but if you had an accessible object g (global or not) with a method Update, wherever g was accessible g.Update() would work.


'this' refers to the object used to call the function, which, by default, is 'window'..

This means that any 'static' variables as you call them are all members of the window object. Doing g.Update() is equivalent to doing window.g.Update() or this.g.update() (the last one works ONLY when this line of code is not wihtin a function or another object).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜