I realise that it is useful (for performance reasons) to do something like…
function Abc(a, b, c) {
var window = window;
So when the code accesses window, it doesn’t need to go up the scope chain to finally find window. The same can be done for document, navigator, etc…
But I’m in the process of rewriting some of the MobiScroll jQuery plugin and found this…
function Scroller(elm, dw, settings) {
...
var elm = elm;
var dw = dw;
...
What are the advantages of reinitialising elm and dw to point to their argument variables ?
I’ve read a lot about accessing arguments being costly, but never read anything about why this might be good practice.
What are the benefits in doing this?
In the past, I’ve deliberately removed this construct from MDN documentation.
I don’t think that’s the reason. For a function declared in global scope, resolving an identifier locally versus globally will be insignificanly faster (and perhaps slower depending on the browser).
Scripts may use something similar to the above to ensure window refers to the expected window object and not some other window on the scope chain, e.g.
In the second example:
Declaring the variables is complete waste of time. Putting an identifier in the formal parameter list of a function declaration or expression is equivalent to declaring it localling with var. Declaring such identifiers as local variables has no effect whatever.
There was an early version of Safari that had an issue with formal parameters that weren’t passed a value, but that was a long time ago and was only an issue in one very specific case.