Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JavaScript is giving access to override the existing properties in built-in object

Tags:

javascript

Generally java-script allows to override (extend the new behavior) any function except those objects which are not frozen or seal. In JavaScript Math is a built-in object. But why JavaScript is giving access to override the existing properties in built-in object ?

Please find screenshot: Initially I find min function is available in Math Object. I have updated "min" property with function. This action replaced the existing code. For more clarity I have deleted the property from "min". Here deletion should remove the extended behavior not the core one. But it is removing core property why?

enter image description here

like image 443
santy Avatar asked May 28 '14 08:05

santy


2 Answers

Extending or modifying native code is called monkey-patching, and it's a design feature rather than a design flaw. Virtually everything is mutable and extensible in Javascript, and therefore you have the power to change fundamentals to suit your own needs (e.g. you could overload the min method so that it works with different variable types than just integers and floats), but with that power comes responsibility, so it's generally not advised to change these standard functions unless you know what you're doing; likewise, you have to be aware that if your JS file will be running on someone else's environment, you may not be able to rely on everything you think you can (however, you should generally be able to expect the usual global methods and properties, which is why you may call the global Object.prototype.keys or Array.prototype.slice rather than expect the method to be on the prototype of any one particular object).

In short, when you delete a function that you've modified, you will be deleting it entirely, not reverting it back to some sort of original state. You basically overwrote the original, and so there's no way of getting it back (except by deleting the code that overwrites it!).

like image 167
user162097 Avatar answered Sep 20 '22 18:09

user162097


Thanks to everyone for responding to my question. I got valuable information from everyone. myself did some analysis on this. I have gone through ECMA-262 Specification. I find some properties like 'E' in Math and their configurations.

According to specification document http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1

15.8.1.1 E

The Number value for e, the base of the natural logarithms, which is approximately 2.7182818284590452354. This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

Then I got to know some of properties of Math we can't delete because of 'Configurable' property. When I executed following code, In retured object I find 'min' property of 'configurable: true'.

Object.getOwnPropertyDescriptor(Math, "min"); Object {value: function, writable: true, enumerable: false, configurable: true}

I agree with user162097 As he said 'it's a design feature rather than a design flaw.'

Thanks

like image 29
santy Avatar answered Sep 22 '22 18:09

santy