For example the Mozilla Developer Network simply uses Array.prototype = function to define polyfills, but this creates an enumerable property that breaks for-in loops.
So I used Object.defineProperty(Array.prototype, { enumerable: false ...
Is there some danger to this? Why is this not the common way?
Here is an example polyfill from MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Polyfill
Here is the same example with defineProperty:
Object.defineProperty(Array.prototype, {
enumerable: false,
value: function(func, thisArg) {
'use strict';
if ( ! ((typeof func === 'Function') && this) )
throw new TypeError();
var len = this.length >>> 0,
res = new Array(len), // preallocate array
c = 0, i = -1;
if (thisArg === undefined)
while (++i !== len)
// checks to see if the key was set
if (i in this)
if (func(t[i], i, t))
res[c++] = t[i];
else
while (++i !== len)
// checks to see if the key was set
if (i in this)
if (func.call(thisArg, t[i], i, t))
res[c++] = t[i];
res.length = c; // shrink down array to proper size
return res;
}
});