Is it possible to change or remove properties of a global object in Javascript?

I was just reading stuff about the global object and was wondering if the property values โ€‹โ€‹of the global object could be changed. I don't know what purpose it would serve, but I'm interested.

Take this code for example:

Infinity = 4;           //Alter the property Infinity of the global object
                        //This doesn't prompt an error...
console.log(Infinity);  //Yet, for some reason it still prints Infinity, instead of 4.

      

Could you also do this:

delete Infinity;
console.log(Infinity)

      

It looks like this is not possible, since Infinity still prints Infinity instead of throwing an undefined error.

+3


source to share


2 answers


It depends - is the property being written / configured or not?

Infinity

is not because it appears in the following console log:

> Object.getOwnPropertyDescriptor(window,'Infinity')
< Object {value: Infinity, writable: false, enumerable: false, configurable: false}

      



However, other global properties such as frames

are configurable:

> Object.getOwnPropertyDescriptor(window,'frames')
< Object {value: Window, writable: true, enumerable: true, configurable: true}

      

So basically it depends on how the property is configured.

+2


source


Good question, but the answer is no. Global objects cannot be modified or deleted. Globals are required and built into JavaScript.



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

0


source







All Articles