0

I'm currently studying javascript by following the book series "you dont know js".

In the book, section "scope & closure", where the author is discussing "IIFE", the author mentioned

Another application of this pattern addresses the (minor niche) concern that the default undefined identifier might have its value incorrectly overwritten, causing unexpected results. By naming a parameter undefined, but not passing any value for that argument, we can guarantee that the undefined identifier is in fact the undefined value in a block of code:

undefined = true; // setting a land-mine for other code! avoid!

(function IIFE( undefined ){

  var a;
  if (a === undefined) {
      console.log( "Undefined is safe here!" );
  }

})();

But I thought undefined is a non-writable variable. And when I tried undefined = true; in chrome browser, the value of undefined indeed did not change (i.e. the assignment failed silently under non-strict mode). So why is the author trying to assign to undefined ?

And secondly, I'm not really sure what is the purpose of IIFE function with undefined as its parameter? Like what are some of the potential uses?

Thor
  • 9,638
  • 15
  • 62
  • 137
  • 2
    *"So why is the author trying to assign to `undefined` ?"* `undefined` wasn't always read-only. Back in the day to avoid breakage of your code because somebody messed with `undefined`, you would simply create your "own version". – Felix Kling Jul 06 '17 at 21:48
  • 1
    This changed in a later ES version - there are *legacy questions* that deal with it. ES3 has not such guarantees - not sure if it was ES5 or ES6 that "corrected" the situation. – user2864740 Jul 06 '17 at 21:49
  • 1
    also you can set a variable to the name undefined as well, which if you have a certain scope of things could ruin everything. – Jhecht Jul 06 '17 at 21:50
  • 2
    Your second question is answered by the quote you pasted: "*By naming a parameter `undefined`, but not passing any value for that argument, we can guarantee that the `undefined` identifier is in fact the undefined value in a block of code*" – melpomene Jul 06 '17 at 21:50
  • @FelixKling ah, ok, so `undefined` used to be writable, but now its just a read only property. Make sense – Thor Jul 06 '17 at 21:51
  • 3
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined says: "*In modern browsers (JavaScript 1.8.5 / Firefox 4+), `undefined` is a non-configurable, non-writable property per the ECMAScript 5 specification.*" So apparently before ES5 `undefined` was writable. – melpomene Jul 06 '17 at 21:52
  • 1
    Also you don't really know in which browsers your code runs. Not that I think these days people run browsers that are that old, but ES5 took a while to be adopted. – Felix Kling Jul 06 '17 at 21:52
  • @melpomene I understand why the author came up with IIFE with `undefined` as a parameter, but I guess what I was trying to ask is: what are some of the practical useage for such thing? – Thor Jul 06 '17 at 21:52

0 Answers0