Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override window and undefined in javascript? [duplicate]

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.

like image 676
Sachin Avatar asked Oct 17 '25 03:10

Sachin


1 Answers

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!