兼容问题后续---------
一些JS的原生函数也不被IE8支持的解决办法(其实就是重写方法):
类似于数组的indeOf()方法:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (elt) {
//获取数组长度向下取整的数值,这里用位移运算符是为了确保返回的是非负整数
var len = this.length >>> 0;
//获取这个函数的第二个参数的数值,也就是从第几个下标值开始索引,如果没有,那么默认为0
var from = Number(arguments[1]) || 0;
//对第二个参数进行处理,如果大于等于0,向下取整;如果小于等于0,向上取整
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
//将负数转换成反向含义
if (from < 0)
from += len;
for (; from < len; from++) {
//格式:(变量 in 对象)......注意
//当“对象”为数组时,“变量”指的是数组的“索引”;
//当“对象”为对象是,“变量”指的是对象的“属性”
//当from满足数组的索引并且传入的elt等于该数组对应索引下的数值时,返回该索引
if (from in this &&
this[from] === elt)
return from;
}
//找不到,返回-1
return -1;
};
}
方然,其他的方法类似:
Array的map()方法:
if (!Array.prototype.map) {
Array.prototype.map = function (callback, thisArg) {
var T, A, k;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0;
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
A = new Array(len);
k = 0;
while (k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[k];
mappedValue = callback.call(T, kValue, k, O);
A[k] = mappedValue;
}
k++;
}
return A;
};
}
Array的filter()方法:
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun ) {
var len = this.length;
if (typeof fun != "function") {
throw new TypeError();
}
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this) {
var val = this[i];
if (fun.call(thisp, val, i, this)) {
res.push(val);
}
}
}
return res;
};
}
~~~~~~~~~~~~~~~~~~~~~~~~~这是未完待续分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~