一、call()
-
ES6之前
Function.prototype.myCall = function (_context){ // _context空检测 _context = _context? _context: window; // 获取函数参数 let args = []; for(let i=1, len=arguments.length; i<len; i++){ // 避开在eval时组装参数中字符串的麻烦 args.push('arguments[' + i + ']'); } // 将函数挂到_context上 _context.__fn = this; // 执行函数 let result = eval('_context.__fn(' + args + ')'