Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upArrow functions do not .bind() #1043
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Donskelle
Dec 7, 2017
Yeah thats a feature of es2015. They don't want us to bind contexts around. Use arguments
Donskelle
commented
Dec 7, 2017
|
Yeah thats a feature of es2015. They don't want us to bind contexts around. Use arguments |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Dec 8, 2017
Member
@TejasQ a similar "issue" exists with function foo() { }.bind(this).bind(this) - if it doesn't fail the second time, then (() => {}).bind(this) shouldn't fail.
Similarly, var self = this; function foo() { self.something(); }.bind(this) doesn't have the effect you intend, but that couldn't be made to fail - in other words, using .bind has always required you to have intimate knowledge about the implementation of the function to guarantee an effect, including whether it's already bound, or if it's an arrow function, or it it even references this at all.
|
@TejasQ a similar "issue" exists with Similarly, |
littledan
added
the
question
label
Dec 8, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
claudepache
Dec 8, 2017
Contributor
I guess (disclaimer: I don’t know Scala) that the core issue is that it is not possible to bind a new this-value to a function whose this-value is already bound. Instead, Function#bind will just silently ignore the provided value.
In the particular case, the this-value of the arrow function is already bound to the this-value of its enclosing scope.
There is some logic behind that behaviour, but you need good understanding of the JS function model in order to grasp it, and it may be unexpected for people accustomed to other models.
|
I guess (disclaimer: I don’t know Scala) that the core issue is that it is not possible to bind a new this-value to a function whose this-value is already bound. Instead, Function#bind will just silently ignore the provided value. In the particular case, the this-value of the arrow function is already bound to the this-value of its enclosing scope. There is some logic behind that behaviour, but you need good understanding of the JS function model in order to grasp it, and it may be unexpected for people accustomed to other models. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Kovensky
Dec 21, 2017
Forbidding .bind at all is also a bad idea because you can still use .bind for partial application.
const add3 = (a, b, c) => a + b + c
const add2 = add3.bind(undefined, 0)
const plus5 = add2.bind(undefined, 5)
plus5(3) // 8
Kovensky
commented
Dec 21, 2017
|
Forbidding const add3 = (a, b, c) => a + b + c
const add2 = add3.bind(undefined, 0)
const plus5 = add2.bind(undefined, 5)
plus5(3) // 8 |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
This seems answered; @TejasQ, is it ok to close? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
TejasQ
Jan 4, 2018
Indeed. Though I'd reiterate as a closing thought that a greater deal of verbosity here could help. Thank you all for your consideration!
TejasQ
commented
Jan 4, 2018
|
Indeed. Though I'd reiterate as a closing thought that a greater deal of verbosity here could help. Thank you all for your consideration! |
TejasQ commentedDec 7, 2017
•
edited
Hello,
A coworker of mine unfamiliar with Javascript (he's a Scala dude) pointed out to me that this code right here
works a little quirky: though we're explicitly binding a context to the lambda, not only does it fail, which is understandable given their lexical
thiscontexts, but it gives no feedback to the developer.Not sure if this is a browser issue or a language issue, but I thought I'd start a conversation about this. Please let me know if this is better suited to the web platform as opposed to TC39 and I'll gladly close this and open it elsewhere.
Thank you for reading!❤️