7

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

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
santy
  • 310
  • 1
  • 9
  • Because that's the way it's designed. (Don't know why, but it is.) It even appears the entire `Math` object can be overridden. Possibly to allow for better, more accurate custom implementation of it's functions. – Cerbrus May 28 '14 at 09:01

4 Answers4

3

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!).

user162097
  • 1,250
  • 11
  • 21
  • 1
    [Actually there are arcane ways to bring back the dead](http://stackoverflow.com/questions/8580431/recovering-built-in-methods-that-have-been-overwritten), but +1 to the rest of your answer! – Bergi Jul 09 '14 at 14:09
2

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

santy
  • 310
  • 1
  • 9
1

No it is working correctly. delete function is deleting property from your Object. It doesn't know the object was original one or overridden, that's why overriding built in functions behavior is not best practice. Instead of changing the native function behavior you can create yours in that object prototype, for instance lets create remove function for Array object:

Array.prototype.remove =  function(member) {
  var index = this.indexOf(member);
  if (index > -1) {
    this.splice(index, 1);
  }
  return this;
}

Nevertheless you can inherit from native objects, more about this you can read in this article-inheriting from native objects.

Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41
Narek Mamikonyan
  • 4,601
  • 2
  • 24
  • 30
0

One nature of scripts is a fast prototyping. You may sort this abillity to a non-strict-declaration-aspect of javascript.

Grim
  • 1,938
  • 10
  • 56
  • 123