Search code examples
javascriptactionscriptconstructorecmascript-5jscript

Is there any practical use of redefining Math.constructor in JavaScript/ActionScript?


The Math object does not have a prototype property, but does have a constructor property. Is there any case in which redefining the constructor would be useful?


Solution

  • MDN says:

    Unlike the other global objects, Math is not a constructor. All properties and methods of Math are static.

    In other languages, when a class is static, you can directly use its properties and methods without creating an instance of that class ( an object ). If Math constructor is used, there is no native type to support the object, unlike with the primitive types: Number, String, Boolean. They can be converted to objects with their wrappers.

    Furthermore it is a bad practice to extend a root object. If in the future new functionality is implemented in the environment and the code don't have fail-safety check for this, it will override the native one.

    My personal opinion is that you do not constructor, nor prototype - you can define your own mathematical functions. The Math object is here just to present a standard functions, and give programmers the leverage not to define Pi or E, for example. And probably user defined mathematical function will be several times slower than the built-in.