模拟call方法:
Function.prototype.mycall = function (context) {
var context = context || window;
context.fn = this;
var args = [];
for (var i = 0, len = arguments.length; i < len; i++) {
args.push('arguments[' + i + ']');
}
var result = eval('context.fn(' + args + ')');
delete context.fn;
return result;
}
模拟apply方法:
Function.prototype.myapply = function (context, arr) {
var context = context || window;
context.fn = this;
var result;
if (!arr) {
result = context.fn();
}
else {
var args = [];
for (var i = 0, len = arr.length; i < len; i++) {
args.push('arr[' + i + ']');
}
result = eval('context.fn(' + args + ')')
}
delete context.fn
return result;
}
模拟bind方法:
Function.prototype.mybind = function (context) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var fNOP = function () { };
var fBound = function () {
var bindArgs = Array.prototype.slice.call(arguments);
return self.apply(this instanceof fNOP ? this : context, args.concat(bindArgs));
}
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
}