-
Notifications
You must be signed in to change notification settings - Fork 103
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
ES6 Arrow Functions #264
Comments
No worries :) This is not a valid code. Here I fixed it: const stamp = stampit().methods({
error: console.error,
incorrect(value) { // < -THAT LINE
this.error(`value ${value} is incorrect`);
}
}); Now the Cheers! |
Thanks @koresar for your quick answer but why not use the same approach than const stamp = stampit().methods(({ instance, stamp, args }) => ({
error: console.error,
incorrect: (value) => {
instance.error(`value ${value} is incorrect`);
}
));
|
@koalabz You really like that better than what @koresar showed you? It's terribly verbose :) Personally I don't use fat arrows with stampit at all to avoid confusion and errors. Either way it's not really possible because in the time when you are calling If you are familiar with AST and writing Babel plugins, you can perhaps make the one that can handle this properly |
I don't prefer one or the other i just try to understand why we can't use ES6 Arrow in other methods than
It's the same when you call
:) |
So, yeah. Daniel is correct. Also, don't put arrow functions everywhere. |
We can't use arrow functions as methods because arrow functions bind the "this" context to the upper scope. In your case it's the "undefined". |
@koalabz Check this out :) const stamp = stampit().methods({
oldStyle: function() { ... },
fatStyle: () => { ... },
newStyle() { ... },
}); So regarding your quote about arrow function bringing simplicity, I am sure you can see that in this case it only brings chaos :) Closing as it's not possible to solve this on library level. |
I just wanted to second @koalabz's suggestion because I would prefer never to use |
@neverfox could you please show us code example of the proposed syntax? Otherwise, I struggle to understand code not seeing code. :) |
Arrow functions do not bind In other words, if you want to declare a method that uses the instance, you should chose one of these options:
If you want to avoid |
@neverfox You can already avoid That would be a breaking change, it would dramatically alter the behavior of all methods, cause a performance hit for every method call, and cause a TON of confusion. In other words, it's not gonna happen. |
First of all, thanks for this amazing piece of code.
I'm starting playing with stampit and i'm stuck trying to use arrow functions in
methods()
function like this :I understand that
this
do not point to the stamp object itself.Is there anyway to get the same arguments :
{ instance, stamp, args }
like theinit()
function ?The text was updated successfully, but these errors were encountered: