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.
Arguments, resembling variables, are used to pass data from one workflow to another. They can store data dynamically and pass it on. One difference between variable and argument is that variables pass data to other activities while arguments pass data to other workflows.
Reassigning the value of an argument variable mutates the arguments object. Mutation is the act of changing the source or the original element. Misuse of mutability can cause side effects to your software.
The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0 .
Parameters allow a function to perform tasks without knowing the specific input values ahead of time. Parameters are indispensable components of functions, which programmers use to divide their code into logical blocks.
> 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.
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.
var fn = (function(window) {
// In here window is unequivocally a reference to the global object
}(this));
In the second example:
> function Scroller(elm, dw, settings) {
> ...
> var elm = elm;
> var dw = dw;
> ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With