Most built-in JavaScript types are constructors whose prototypes contain the methods and other properties that define their default behavior:
//(results will vary by browser) Object.getOwnPropertyNames(Function.prototype) //["bind", "arguments", "toString", "length", "call", "name", "apply", "caller", "constructor"]
You can’t delete or replace a native prototype, but you can edit the values of its properties, or create new ones:
//create a new array method that removes a member Array.prototype.remove = function(member) { var index = this.indexOf(member); if (index > -1) { this.splice(index, 1); } return this; } ['poppy', 'sesame', 'plain'].remove('poppy'); //["sesame", "plain"] ['ant', 'bee', 'fly'].remove('spider'); //["ant", "bee", "fly"]
Et voila! Our code gets a useful array extension for free. However if you brag about doing this in production code, expect to get pummeled by a wave of fierce disapproval. Some of it carries weight. Let’s sift the danger from the dogma and try to reach an honest conclusion: