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
// apply Function.prototype.selfApply = function (context, args) { context = context || window args = args ? args : [] const key = Symbol() context[key] = this // 此时的this 就是调用的selfApply的function const res = cotext[key](...args) delete context[key] return res } // call 相对 apply的区别就是参数可以任意 // ...args 转成数组args Function.prototype.selfCall = function (context, ...args) { context = context || window args = args ? args : [] const key = Symbol() context[key] = this // 此时的this 就是调用的selfCall的function const res = cotext[key](...args) delete context[key] return res } // bind Function.prototype.myBind = function (context, args) { context = context || window args = args ? args : [] const fn = this // 此时的this 就是调用的myBind的function return function newFn(...newArgs) { // 要考虑要返回的函数可能会调用new ,这里要判断是否要调用 if(this instanceof newFn) { return new fn(...args, ...newArgs) } return fn.apply(context, [...args, ...newArgs]) } }
面试感悟,手写bind,apply,call
The text was updated successfully, but these errors were encountered:
No branches or pull requests
参考
面试感悟,手写bind,apply,call
The text was updated successfully, but these errors were encountered: