3

I have seen some examples like this

(function($, window, undefined) {
  ...
  // Do awesome stuff
  ...
})(jQuery, this);

I understand passing jQuery as a paramter and receiving as $.

This is done to avoid conflict between jquery's $ and any global variable $ ( defined by mistake or by some third party library ).

Why do people pass this and receive as window and also receive undefined in function parameter ?

Is there any way we can override window and undefined ?

PS: I have already tried this in Chrome

undefined = 2; // 2
undefined == 2; // false

It proves undefined can not be over-ridden.

sachinjain024
  • 19,669
  • 27
  • 88
  • 154
  • 2
    better take a look at this [answer](http://stackoverflow.com/a/2716188/1671639) – Praveen Mar 26 '14 at 04:27
  • This question has already been answered for you http://stackoverflow.com/questions/5021133/get-undefined-if-window-undefined-is-overwritten – HelloWorld Mar 26 '14 at 04:28
  • @Praveen Thanks for the link. It says undefined can be changed but I am not able to over-ride it. – sachinjain024 Mar 26 '14 at 04:28
  • 1
    @blunderboy yes it can be overwritten only within a function scope like here in a closure `(function() { console.log(undefined); //undefined var undefined = 10; console.log(undefined);//returns 10 })();` – Praveen Mar 26 '14 at 04:32
  • @C-link It is not duplicate because I want to ask **How to override undefined and window variables** in specific ? – sachinjain024 Mar 26 '14 at 04:58

1 Answers1

9

Try this

function test () {
    var undefined = 2; // 2
    console.log(undefined == 2); // true

    var window = 5; // 5
    console.log(window == 5); // true
}

I believe you can alter the value of undefined and window inside a function, but not in the global scope

SajithNair
  • 3,821
  • 1
  • 15
  • 23