3 ms·
> So a CoffeeScript => is now output as => Wait, using the arrow function notation doesn't bind this, arguments, super, or new.target[1] in javascript, as oppo
by kbenson 9y ago
> So a CoffeeScript => is now output as =>
Wait, using the arrow function notation doesn't bind this, arguments, super, or new.target[1] in javascript, as opposed to the normal function syntax. Not being a user of coffeescript, did coffeescript not do that already, or did they also throw in code to change that in the generated code?
It seem to me it would make sense to map items that match conceptually, not just notationally (unless it's common to have both coffeescript and JS side by side). Can someone comment on how this is handled?
1: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Edit: Added "in javascript" after reference to make it obvious I was talking about JS behavior for the binding.
- thechewanater 9y agoIn coffeescript 1.x fat arrow functions already bound "this", although binding "arguments" is an actual change. http://coffeescript.org/#breaking-change-fat-arrow http://coffeescript.org/#breaking-change-fat-arrow
- Nullabillity 9y agoCoffeeScript did already bound `this` for `=>`. There is also `->`, which is morally equivalent to JS' old-style `function`.
- madeofpalk 9y agoCoffeescript's => binds this. Javascript's => also binds this (more or less) Seems to map fairly well conceptually.
- kbenson 9y ago> Javascript's => also binds this (more or less) Javascript's => doesn't bind this though, and function does, which was my point (and is what the reference I included was saying). That's a specific change in JS, and either coffeescript had code in place to emulate the current behavior in prior versions where they used function(), or they have code in place now to emulate function() now that they're using =>. I'm going to update my original comment to be a bit more clear on what I was saying.
- irishsultan 9y agoOld Coffeescript behaviour for => was to use a function where this was bound to that of the environment. This seems subtly different from having this not being bound at all (in that you can't rebind it under the new translation), but that's unlikely to have been a common use case. The main other difference seems to be that there is no arguments object for the local function anymore, although once again that's unlikely to be a problem in most functions defined with a fat arrow. As someone else also said, there were two ways to define functions, one using =>, one using -> the latter maps to a normal JS function.
- kbenson 9y ago> Old Coffeescript behaviour for => was to use a function where this was bound to that of the environment. Okay, so it sounds like coffeescript 1.0 => was effectively doing what JS => does now, so it makes sense to move to the newer JS syntax Thanks for elaborating! (It's interesting how "binds this" can, and was interpreted in multiple ways, Perhaps there's a better, more specific wording I could have used to cut through those misunderstandings, whether they be with those that replied or me reading their replies).
- madeofpalk 9y ago> Javascript's => doesn't bind this though I think we're getting our wires crossed. Both Javascript and CoffeeScript have (more or less, for the common use cases) the same `this` behaviour for =>. It preserves the `this` value from the context.
- kbenson 9y agoYes, that's what was explained in the sibling. There's a lot of confusion on what "binds this" can mean, and while I thought you might have been misinterpreting me, I may have been misinterpreting you. I was using the terminology that MDN uses in reference to the function arrow, but in the context of this question, some additional elaboration might have been useful to distinguish exactly what "this" is when it's bound (the calling environment, or the current subject being called on as is often the case).