简体   繁体   中英

How to assign value to undefined

Todd mentioned in his article that it is possible to assign a value of undefined(as a reason to put the object into arguments without assigning it):

In ECMAScript 3, undefined is mutable. Which means its value could be reassigned, something like undefined = true; for instance, oh my! Thankfully in ECMAScript 5 strict mode ('use strict';) the parser will throw an error telling you you're an idiot. Before this, we started protecting our IIFE's by doing this:

(function (window, document, undefined) {

})(window, document);

Which means if someone came along and did this, we'd be okay:

undefined = true;
(function (window, document, undefined) {
    // undefined is a local undefined variable
})(window, document);

However, I tried to assign it without any luck:

$ undefined = true
$ true
$ undefined
$ undefined

Can anyone say how to do that? Just out of curiosity. Or new browsers dont allow this anymore?

Putting as an answer:

(function(undefined){

    console.log("\"undefined\" parameter:", undefined);

})("test");

It will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM