Function.prototype.call = function() {
var ctx = arguments[0] || window
var args = []
for(var i = 1; i < arguments.length; i++){
args.push('arguments[' + i +']')
}
ctx.fn = this
var result = eval('ctx.fn(' + args +')')
delete ctx.fn
return result
}
Function.prototype.apply = function(ctx, arr) {
var ctx = ctx || window
var args = []
for(var i = 0; i < arr.length; i++){
args.push('arr[' + i +']')
}
ctx.fn = this
var result = eval('ctx.fn(' + args +')')
delete ctx.fn
return result
}
function foo () {
console.log('this.name:', this.name)
console.log('arguments:', [...arguments])
return 'I am back.'
}
const obj = {
name: 'obj name'
}
const result = foo.apply(obj, [1, 2, 'c'])
console.log(result)
console