-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Proposal: Deriving a function from an instance method that is bound/closed-over the instance – instance:method #4512
Comments
FTR, this feature is available in Coco/LS. |
@vendethiel, respectfully, I use Coffeescript because I think it strikes the right balance between being concise and expressive... :> Looks like this is related to #3634 @@method # may be a bit clearer in intent The above looks nicer, but you can't use it to bind over 'any instance' - only the current Same as in that issue, I would like this for attaching bound functions as event handlers in node. |
I just wanted to point out a case in the wild, and your issue link is great for that.
Well, not completly. The suggestion wants |
Using it within the definition of a class would look like this: class Handler
constructor: (node) ->
$(node)
.on('click', @:on_click)
.on("mouseover", @:on_mouserover)
on_click: -> #...
on_mouseover: -> # ... This is how it would be used confusingly: instance:a.b.d instance.a.b.d.bind(instance) What's on the left of the instance.a.b:d instance.a.b.d.bind(instance.a.b) And then from the global context... :console.log window.console.log.bind(window) The special cases are having I think this would create ambiguity when defining ranges. An alternate could be: instance@method |
I'm not sure why we need a shorthand for this. |
@GeoffreyBooth - because we all have too much code which looks like this: result = someList.map (item) =>
@someMethod(item) Instead of: result = someList.map(this::someMethod) We could use |
Well, Please also show what your new proposed syntax(es) would compile into. |
Ah, I didn't even know this CoffeeScript syntax As for the compiled output - it could either be: obj.~method compiles to: obj.method.bind(obj) Or use Babel's bind operator. Is there any issue with any of those? I guess the first one is preferable as it works everywhere without more transpilation. |
Babel’s
|
Yes, exactly because this has been discussed before (actually many times) means that a lot of people need/want this. I also use result = someList.map (item) =>
@someMethod(item) Is way prettier than: result = someList.map(@someMethod.bind(this)) My 2 cents are - this is something I've personally wanted for a very long time and in my code I would use it a lot. I also do think that many people are like me (the at least 3 issues on the same topic over the years are a testimony for that I guess). |
I agree this could be a valuable addition, especially given the loss of bound methods. Whilst you're right that items.map console.~log
# vs
items.map console.log.bind console
# vs
items.map (item) -> console.log item I tend to end up using this most often when writing class wrappers around event-driven components, e.g. el = React.createElement
class Widget extends React.Component # or a Stream or a Socket or ...
render: ->
onClick = @handleClick.bind @
# When there are multiple events to be handled, this boilerplate adds up very quickly
el 'div', { onClick }, 'Click me!'
handleClick: ->
doSomethingWith @ |
I think an anonymous function is clearer. items.map console.log.bind console
# means:
items.map (item, index, array) -> console.log item, index, array
# not:
items.map (item) -> console.log item |
Ah, good point (and one I've been bitten by before, in fact!). On the other hand, if you did want to forward all the arguments, there's even more duplication: items.forEach console.~log
# vs
items.forEach -> console.log arguments... # relies on 'arguments', which may be undesirable
# vs
items.forEach (args...) -> console.log args... # duplication
# vs
items.forEach console.log.bind console # duplication
# vs
items.forEach (currentValue, index, array) -> console.log currentValue, index, array # duplication Regardless, all the use-cases are pretty much centred around the one case of 'binding lots of callbacks', which I agree isn't such a common case as to definitely merit syntax. It is a confusing aspect of JS for people new to the language, and often a nuisance for experienced JS users, so having syntax for it could be quite widely appreciated. |
For this to advance, it needs a specific syntax proposed, including what that syntax compiles to; explanations of how this new syntax handles the edge cases discussed in #2136; and why this should be accepted when #2136 wasn't (i.e. what makes this different than that, or why this shouldn't be closed for the same reasons). |
Closing as incomplete (see previous comment). If this gets fleshed out, and the community expresses interest, we can reopen. |
Hello -
I was hoping to propose a syntax that would allow you to create a function with a bound/closed-over "this".
Would be:
Please forgive me if I got something wrong here... I usually wind up submitting proposals when I'm too tired and there are often typos.
I believe Babel has something like this called the Bind operator:
https://babeljs.io/docs/plugins/transform-function-bind/
@vendethiel: You closed my other issue from yesterday but I corrected some typos :( Please go back and have another look?
The text was updated successfully, but these errors were encountered: