Function.prototype.bind2 = function (context){
let self = this;
let args = [].slice.call(arguments,1) //闭包存取参数
return function(){
let _args = [].slice.call(arguments)
return self.apply(context,args.concat(_args))
}
}
// 为上下文添加函数 字符创创建参数 eval执行函数 删除函数 call参数是多个,apply参数是数组
Function.prototype.myCall = function (context){
var context = context || window;
context.fn = this
let args = []
for(let i = 1; i < arguments.length; i++){
args.push('arguments[' + i + ']')
}
eval('context.fn(' + args + ')')
delete context.fn
}
Function.prototype.myApply = function (context,arr){
var context = context || window;
context.fn = this
if(!arr){
context.fn()
}else{
let args = []
for(let i = 0; i < arr.length; i++){
args.push('arr[' + i + ']')
}
eval('context.fn(' + args + ')')
}
delete context.fn
}
var foo = {
value: 1
};
function bar(name, age) {
console.log(name)
console.log(age)
console.log(this.value);
}
bar.myCall(foo, 'kevin',18);
console