JavaScript is allowed for basic types of expansion functionality. For example, you can make this method available to all objects by adding a method to Object.prototype. Such a way applies to functions, arrays, strings, numbers, regular expressions, and Boolean values.
For example, all functions can be available by adding methods to the Function.prototype:
function (Name, func) { this. Prototype[name] = func; return This ; }
By adding method methods to Function.prototype, then adding methods to call the Mehod method directly, without having to write prototype. For example, JavaScript does not have an shaping type, and we can add an integer method class implementation to Number.prototype.
function () { return math[ this < 0? ' Ceil ': "Floor"(this);}); Console.log (( -10/3). Integer ()); Out Put-3
By adding methods to basic types, the expressive force of language can be greatly improved. Because of the dynamic nature of JavaScript archetype inheritance, new methods are immediately assigned to all object instances, including objects that were created before the method was added.
Because primitive types are common constructs, be careful when using class libraries. An insurance policy is to add it only when you are sure that the method is not there, for example:
function (Name, func) { if (! this. prototype (name)) { this. Prototype[name] = func; } return This ;};
JavaScript Learning Notes: Extending the functionality of a type