Skip to content
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

Open
shfshanyue opened this issue Nov 15, 2019 · 8 comments
Open

【Q032】js 中什么是 softbind,如何实现 #33

shfshanyue opened this issue Nov 15, 2019 · 8 comments
Labels

Comments

@shfshanyue
Copy link
Owner

shfshanyue commented Nov 15, 2019

相关问题:

@shfshanyue shfshanyue added the js label Nov 15, 2019
@newwangyiyang
Copy link

bind函数多次调用会已第一次绑定的this为准,softbind已最后一次绑定传入的this为准;

@miaooow
Copy link

miaooow commented Jan 5, 2021

  Function.prototype.softBind = function(obj, ...rest) {
    const fn = this
    const bound = function(...args) {
      const o = !this || this === (window || global) ? obj : this
      return fn.apply(o, [...rest, ...args])
    }

    bound.prototype = Object.create(fn.prototype)
    return bound
  }

function foo() {
console.log(name: ${this.name});
}

let obj = {name: "obj"};
obj2 = {name: "obj2"};
obj3 = {name: "obj3"};

let fooBJ = foo.softBind(obj);
fooBJ();  // name: obj   这个时候软绑定已经生效了,this绑定到obj上
obj2.foo = foo.softBind(obj);
obj2.foo(); //name: obj2   这里已经的this隐式绑定到obj2上了
fooBJ.call(obj3); // name: obj3  这里this被硬绑定到obj3上了
setTimeout(obj2.foo, 100); // name: obj  软绑定了最初的obj

@shfshanyue
Copy link
Owner Author

TODO

@shuangcherry
Copy link

bound.prototype = Object.create(fn.prototype); 为什么要有这一句,不是很懂

@shen076
Copy link

shen076 commented Sep 6, 2022

  Function.prototype.softBind = function (obj, ...args) {
     const fn = this;
     return function (...args2) {
        return fn.apply(this === global ? obj : this, args.concat(args2));
     };
  };

@zhangtuo1999
Copy link

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
}

@rujptw
Copy link

rujptw commented Mar 13, 2023

bound.prototype = Object.create(fn.prototype); 为什么要有这一句,不是很懂
是要考虑到new的情况来着

@Yeti-xxx
Copy link

实现一个Bind:

function person(a, b, c, d, e) {
    console.log(this.name, a + b + c);
    console.log(d,e);
}

const yeti = {
    name: 'yeti'
}

// 实现一个bind
Function.prototype.newmyBind = function (obj, ...args) {
    const that = this
    return function (...rest) {  //返回一个修改好this的函数
        // ...rest来自返回函数传入的参数  ...args是调用Bind时传入的参数
        that.call(obj, ...args, ...rest)
    }

}

const resBind = person.newmyBind(yeti, 1, 2, 6) //1 2 6 就是...args
resBind(5, 6)   //传入...rest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants