Using function.prototype.bind without breaking function.prototype.apply

Mark S. Miller erights at google.com
Wed Sep 7 14:52:08 PDT 2011


On Wed, Sep 7, 2011 at 2:46 PM, Jake Verbaten <raynos2 at gmail.com> wrote:

> var f = function() { return this; }
>
> var g = f.bind(???);
>
> g.apply({ "foo": "bar" }).foo // expect "bar"
>
> Function.prototype.bind set's the thisArg to a certain value. Is there any
> way to leave it flexible to be overwritten using `.call` and `.apply` later
> on?
>
> Is there any ES:Harmony/strawman proposal for this?
>
> If there is not I propose using `undefined` to tell Function.prototype.bind
> to leave thisArg flexible.
>

We cannot use 'undefined' for this, as we already use f.bind(undefined) in
order to ensure that f's "this" is useless, and therefore harmless.

Altogether, what you're asking for is similar to <
http://wiki.ecmascript.org/doku.php?id=strawman:soft_bind>, which, as you
see, can be implemented as a JS library. I suspect your weaker bind could
use the same technique, and so needs no new support from the language.


> The use case would be using .bind to curry parameters without effecting
> this.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110907/30774430/attachment-0001.html>


More information about the es-discuss mailing list