Syntactic sugar for using a function as if it were a method of its first argument

Tab Atkins Jr. jackalmage at gmail.com
Tue May 27 12:51:03 PDT 2014


On Tue, May 27, 2014 at 12:40 PM, Jasper St. Pierre
<jstpierre at mecheye.net> wrote:
> From my reading of the email and strawman page, let f = obj::foo; is exactly
> equivalent to let f = foo.bind(obj);
>
> Am I wrong? How is the result subtly different?

Brendan's saying that the return value of foo.bind(obj) is subtly
different from foo itself.

> Really, with "obj::foo", I would expect "obj::foo" to be the same as
> "obj.foo.bind(obj);", not "foo.bind(obj);" And even then, I don't think it's
> worth it for a new syntax, since we already have the automatic method
> closures for the new class syntax in ES6.

It is technically the same, but when you write `obj::foo()`, the
function is immediately called and thrown out in favor of the
function's return argument.  This means it's also identical to
foo.call(obj), which is cheaper as there's no allocation involved.

~TJ


More information about the es-discuss mailing list