Will the native JavaScript constructors/functions like Function, Object, Array always be available in any scope? Are they inherited by the global object or something like in the same level or even higher?
As an example, MooTools contains the following line in its self executing function:
var Function = this.Function;
So I asked myself, is there any situation where these native constructors become unavailable? Maybe in server side or strict JS? Or is it just a performance question to cache a local reference? But if so, why not doing it this way:
var Function = Function;
They have the same name, so you can’t do that.
As to why it’s done in the first place? Supposedly to guard against user error or redefinitions, and also to reduce the amount of the scope chain the engine has to look up to find something. Relevant blog post: http://blog.minite.ch/?p=47
Of course,
Functionisn’t read-only, so even if someone did redefineFunctionin the global namespace that wouldn’t help. Anybody who does this deserves it, though =)EDIT: If they actually did want to protect against user error, a foolproof way would be:
But it’s pointless, as stated above.