apply,call有什么用,怎么用我就不在这里介绍了,自己可以看看资料,可以告诉它们很有用就是.罗嗦的话不说,直接看实现代码吧
if (typeof(parseInt.apply) != 'function') {
Function.prototype.apply = function (obj, argu) {
var s;
if (obj) {
obj.constructor.prototype._caller = this;
s = "obj._caller";
} else {
s = "this";
}
var argus = new Array();
for (var i=0;i<argu.length;i++)
argus[i] = "argu[" + i + "]";
eval("var r = " + s + "(" + argus.join(",") + ");");
return r;
};
Function.prototype.call = function (obj) {
var argu = new Array();
for (var i=1;i<arguments.length;i++)
argu[i-1] = arguments[i];
return this.apply(obj, argu);
};
}
先实现apply,再用apply实现call.加上parseInt.apply来判断看是否是高版本,已经支持apply和call.