We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function.prototype.call2 = function(context){ let args = []; context= context? Object(context) : window; for(let i=1;i<arguments.length;i++){ args.push('arguments['+i+']') } context.fn = this; let result = eval('context.fn('+args+')');//因为'context.fn(' + args +')'本质上是字符串拼接,会自动调用toString()方法 delete context.fn return result }
Function.prototype.apply2 = function(context, arr){ let args = []; context= context? Object(context) : window; context.fn = this; let result; if(arr){ for(let i=0;i<arr.length;i++){ args.push('arr['+i+']') } result = eval('context.fn('+args+')');//因为'context.fn(' + args +')'本质上是字符串拼接,会自动调用toString()方法\ }else{ result = context.fn(); } delete context.fn return result }
参考自木易杨大佬的文章
The text was updated successfully, but these errors were encountered:
No branches or pull requests
1、模拟call的实现
2、模拟apply的实现
The text was updated successfully, but these errors were encountered: