一个函数一旦用 bind 绑定,它的 this 就不能再被修改了?

Once a function is bound with `bind`, its `this` cannot be modified anymore?

只是稍微玩了一下 JS,我想知道为什么下面的代码输出 "foo" 而不是 "bar":

String.prototype.toLowerCase.bind("FOO").call("BAR")

根据我的理解,.bind("FOO") returns 一个函数 "FOO" 对应 this,因此调用 .bind("FOO")() 输出 "foo"

但是,.call("BAR")this调用了带有"BAR"的函数,所以"bar"应该已经输出了。

我哪里错了?

你是对的。一旦一个函数绑定了 .bind(),它的 this 就不能再被修改。 this 永久 "replaced" 第一个参数为 .bind().

.bind("FOO") returns a function that will have "FOO" for this

不完全是。它 returns 一个函数 绑定 "FOO" for this of toLowerCase。它是这样工作的:

function bind(func, thisArg) {
    return function () {
        return func.call(thisArg);
    }
}

你可以重新绑定返回函数的this你想要的,callfunc(这里:toLowerCase)已经是"hardcoded"成为 thisArg(此处:"FOO")。