Function.prototype.call2 = function(content) { let context = content || window context.fn = this const args = Array.from(arguments).slice(1).map(a =>`'${a}'`) const result = eval(`content.fn(${args.join(',')})`) delete context.fn return result }
apply实现
1 2 3 4 5 6 7 8 9 10 11 12 13
Function.prototype.apply2 = function(content, arr) { let context = content || window context.fn = this let result if (!arr) { result = eval(`content.fn()`) } else { const args = arr.map(a =>`'${a}'`) result = eval(`content.fn(${args.join(',')})`) } delete context.fn return result }