When I open jQuery's source code I find this line.
var
// Will speed up references to window, and allows munging its name.
window = this
Why and how this line will speed up?
From stackoverflow
-
this
would be faster for javascript to reference to, as compared towindow
which would have to be resolved to the window object.Crescent Fresh : That's just what the comment in the code states. -
- javascript functions have lexical scope
- jQuery wraps its entire implementation in an anonymous function
- when said function begins execution it is executing in the "global" scope (ie
this
==window
).
"window = this;"
simply creates a local identifier in that scope so that references to it do not have to "bubble up" outside of the local scope to resolve.David : Thanks. I just noticed that the code's running in a function scope. (function(){var window=this;...})();
0 comments:
Post a Comment