-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
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
【Q032】js 中什么是 softbind,如何实现 #33
Labels
Comments
bind函数多次调用会已第一次绑定的this为准,softbind已最后一次绑定传入的this为准; |
function foo() {
|
TODO |
bound.prototype = Object.create(fn.prototype); 为什么要有这一句,不是很懂 |
Function.prototype.softBind = function (obj, ...args) {
const fn = this;
return function (...args2) {
return fn.apply(this === global ? obj : this, args.concat(args2));
};
}; |
Function.prototype.softBind = function (ctx) {
ctx ??= globalThis
ctx = Object(ctx)
const self = this
const args = [...arguments].slice(1)
function bound() {
fn.call(new.target || this !== globalThis ? this : ctx, ...args)
}
bound.prototype = self.prototype
return bound
} |
|
实现一个Bind:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
相关问题:
The text was updated successfully, but these errors were encountered: