Immutable undefined in self-start function

It has been some time since ECMAScript 5 was released and is fairly well supported in most modern browsers (IE9, CH 19+, FF 4+), and with it "Immutable undefined". Though I keep seeing that "undefined" is passed like this:

(function ( ..., undefined ) {

})(...);

      

From what I can tell Crockford's JSLint tool doesn't really like:

Expected an identifier and instead saw 'undefined' (a reserved word).

      

Even if it is not passed (no arguments in the function call) and it really is an identifier. I understand that his tool is not the bible , we have to follow death, and on the other hand JSHint doesn't seem to care about that.

Is this still considered best practice and how does this affect code performance / safety? Given browser support> = IE9.

+3


source to share


2 answers


Is this still considered best practice and how does this affect code performance / safety? Given browser support> = IE9.

As undefined

, Infinity

is an unprivileged property of the global object, not literal, for example null

, so it can be used as a parameter name without raising the error. Let's look at a function like this

function example(Infinity) {
    return Infinity;
}

      

B example

, is Infinity

no longer a non-writable property and instead acts like any other function parameter, initialized as undefined

if nothing is passed or otherwise takes an argument value. This means that in this function, you cannot simply assume what Infinity

infinity would mean, because it is not, but you could guess what it would mean undefined

.



So, back to the question about undefined

. It will be initialized as undefined

, so the value will remain the same, however now it has lost its immutability inside the function and therefore any typos indicating values undefined

will be carried over by this error. Also, if a parameter is passed to a function, the value will be different from the beginning.

Therefore, to conclude that this does indeed affect security, since undefined

it is no longer "safe" since it has lost its immutable status and can be changed.


It is interesting to note that the wordundefined

is not actually reserved . If it is required for compatibility rather than adding it as a parameter name, I would suggest using a simple one at the top of the function or in the global namespace where you find where it is immutable.undefined

var undefined;

var undefined; undefined = 1; undefined; // undefined

+3


source


If you want to execute jsLint , you can use another variable that is not a reserved word, not undefined

. The following corresponds to jsLint :

(function (window, document, undef) {
    'use strict';

}(window, document));

      

However, you will need to remember the new variable (in this case undef

instead of undefined

). The strict mode above has been added to match jsLint. This is mostly used, so you probably know that the variable you are testing as undefined is actually undefined.

Note that if you are using this example in the text area provided on the jsLint website , you may need to define window

and document

. I was getting two errors until I did this.

var window = window; //global window object
var document = document; //global window object

      



However, I believe that

(function (..., undefined) {
    'use strict';

}(...));

      

perfect fit and more importantly highly recommended.

There are times when reserved words can be changed from their normal states. For example, set undefined

to true

. Paul Ireland discusses this briefly in his video: http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/

0


source







All Articles