wimbuilder2/assets/js/ES5_fix.js

85 lines
2.3 KiB
JavaScript

https://github.com/inexorabletash/polyfill/blob/master/es5.js
// Add ECMA262-5 Array methods if not supported natively
//
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf = function(find, i /*opt*/) {
if (i === undefined) i = 0;
if (i < 0) i+= this.length;
if (i < 0) i = 0;
for (var n = this.length; i < n; i++)
if (i in this && this[i] === find)
return i;
return -1;
};
}
if (!('filter' in Array.prototype)) {
Array.prototype.filter= function(filter, that /*opt*/) {
var other= [], v;
for (var i = 0, n = this.length; i < n; i++)
if (i in this && filter.call(that, v= this[i], i, this))
other.push(v);
return other;
};
}
if (!('forEach' in Array.prototype)) {
Array.prototype.forEach = function(action, that /*opt*/) {
for (var i = 0, n = this.length; i < n; i++)
if (i in this)
action.call(that, this[i], i, this);
};
}
/*
var arr = new Array();
arr.push("abc");
alert(arr.length);
//bad, Object.defineProperty also unsupport
for (i in arr) {
alert(arr[i]); // got indexOf, filter function
}
arr.forEach(function(item, index) { // use forEach() to instead of (for (... in ...)) {}
alert(index);
alert(item);
});
*/
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
}
})()
};