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

Actually bind the function #9

Closed
ForbesLindesay opened this issue Nov 25, 2014 · 11 comments
Closed

Actually bind the function #9

ForbesLindesay opened this issue Nov 25, 2014 · 11 comments

Comments

@ForbesLindesay
Copy link

Shouldn't it be:

Function.prototype[Symbol.referenceGet] = function(base) { return this.bind(base); };

rather than:

Function.prototype[Symbol.referenceGet] = function(base) { return this };

The goal being that:

var a = {
  foo: function () {
    assert(this === a);
  }
};

var foo = a::foo;

foo();

works. Without the binding, the assertion doesn't pass.

@domenic
Copy link

domenic commented Nov 25, 2014

Dupe of #4.

@zenparsing
Copy link
Owner

I think the reasoning is pretty well covered over in the other issue thread.

Also, your example is really about method extraction rather than virtual methods. In the following example, we are going to look up foo as a variable name in the current scope, and then call it with a as the this value.

a::foo();

Syntactic support for method extraction would be nice, but by itself it's not enough of a win to justify new syntax.

In the bind operator strawman, there was something like this for method extraction:

(::obj.foo)

But that's pretty weird.

@domenic
Copy link

domenic commented Nov 26, 2014

Pretty weird ... or pretty AWESOME!? :P

@ForbesLindesay
Copy link
Author

My concern is actually just that I would expect:

(a::foo)() === a::foo()

I find it very surprising that the function call picks up a as the this context. I see why it does, but would find it challenging to explain to someone who was new to JavaScript.

@zenparsing
Copy link
Owner

@ForbesLindesay that's a fair point, and it the intuitive-ness is definitely a high priority. For the sake of argument though, I've noticed some people that are new to JS think that the following should hold:

(a.foo)() === a.foo()

Obviously, it doesn't. And in this proposal :: behaves just like ..

@ForbesLindesay
Copy link
Author

I just don't expect to view :: like .. I expect to see :: much more like an operator, so I equally expect:

var bound = a::foo;
assert(bound() === a::foo());

@zenparsing
Copy link
Owner

@ForbesLindesay Why do you think that is? Do you think someone who had no prior knowledge of :: as a "function bind" operator would think the same?

Thanks for your input, BTW.

@ForbesLindesay
Copy link
Author

I think that a beginner isn't going to be taught :: is the abstract reference operator....

I think they will be taught, :: can be used to bind functions, e.g. a::foo. I think they will then separately be taught, :: can also be used with a weak map to create private variables e.g. a::secret.

How would you explain the a::foo() usage to a beginner, without explaining all the gory details of referenceGet/referenceSet etc.

@zenparsing
Copy link
Owner

I was hopeful that :: could be explained as a "virtual property" operator. It may be, though, that this proposal attempts to use the same operator for use cases that are too-widely-divergent.

I'm currently investigating the idea of splitting up the use cases with different syntax.

@zenparsing
Copy link
Owner

@domenic @ForbesLindesay my current thinking is that it would be better to split up the syntax.

I'm working on a separate private syntax proposal, and I'll also try to put together a more detailed version of the function bind proposal on the ecmascript wiki. I've actually come around to really liking the method extraction syntax: ::console.log.

@zenparsing
Copy link
Owner

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

No branches or pull requests

3 participants