SOURCE

// ex:
var foo ={
  value:1
}

function bar(name,age){
  console.log(name);
  console.log(age)
  console.log(this.value)
}

// bar.call(foo)
// bar.call(foo,'wgf',25)
// 1.首先模拟this改变指向
// Function.prototype.call2 = function(context){
//   context.fn = this;
//   context.fn();
//   delete context.fn;
// }
// bar.call2(foo)

// 2.在1基础上模拟参数
// 3.传空时指向window,增加返回值
Function.prototype.call2 = function(context){
  var context = context||window;
	context.fn = this;
  var args = []
  for(var i = 1;i < arguments.length;i++){
    args.push('arguments['+i+']')
  }
  var result = eval( 'context.fn('+ args +')')
  delete context.fn;
  return result
}

bar.call2(foo,'wgf',25)














console 命令行工具 X clear

                    
>
console