-
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
allow const keyword to evaluate when --nodejs --harmony is used #3571
Comments
+1 |
1 similar comment
+1 |
Honestly, i don't see how this would improve the language. The MDN article on I feel like, as many other recent additions to JS, It'd be nice to have variables be immutable by default, only assigned upon their initialization, and a special form for immutable variables, like But having inventDrink = (name) ->
alcohol = Math.random() * 30
{name, alcohol}
names = ['Cucumber Cognac', 'Vanilla Crush', 'Sugar Nectar']
drinks = (inventDrink name for name in names) So should code done "the right way" be riddled with const inventDrink = (const name) ->
const alcohol = Math.random() * 30
{name, alcohol}
const names = ['Cucumber Cognac', 'Vanilla Crush', 'Sugar Nectar']
const drinks = (inventDrink name for name in names) That'd be ridiculous. That being said, i feel like i'm probably missing something. I'd like to see a good example where the benefits of using |
I agree. Indeed, if we want to go the "right way", we need to use way more |
Yeah probably too late, my impression is that I found this a pretty good write up for let and const - https://strongloop.com/strongblog/es6-variable-declarations/ |
I don't expect to see code using |
@epidemian I have a counter example here, that I just discovered today: defaultData =
nestedProp : 5
data = defaultData # By reading this, it seems quite fine and easy
data.nestedProp += 1 # But this will modify defaultData! |
@maximzavadskiy |
What you're looking for is |
+1 for this |
1 similar comment
+1 for this |
+1 |
you realize there's even a button on github to "+1" now, right? |
Didn't know that, thanks! |
I think coffeescript version
javascript version with strict mode enabled
The developer decides to put a global variable in the window object for required age and references that to block underaged users. Now lets say 6 months later the developer implements functionality to fade all submit buttons out on click. coffeescript version
javascript version with strict mode enabled
WOOPS, looks like the developer accidentally blocks all users from entering a pornagraphic site because they were using coffeescript. However, the es6 javascript compiler warned the developer because const keyword was being used. This is why I would like to see the |
@hooddanielc it's likely that a website config will be hosted on an object. For example, a gambling website could have different limitations per country: const config = {
US: { minAge: 21 },
FR: { minAge: 16 }
}; as explained above, using const here only prevents you from re-assign the variable, it wont prevent mutation. config.US.minAge = 12; // this would work just fine Sadly, |
Yeah keep in mind any checks that are client-side only can be easily bypassed using the developer tools (or any other method). This is not just a coffeescript issue. If you have critical data / checks on your client side code they can and will be bypassed by someone who cares enough. |
I don't understand why people keep giving this deep object example as some kind of counter argument to Why must coffee prevent me from using it even for the very helpful case of avoiding accidental variable name clashes?! |
Because coffeescript doesn't have block scoping. It views variable shadowing as a Bad Part of javascript. See Jeremy's many posts on the topic, there are valid reasons for this decision. |
ok. it was decided not being able to distinguish between variable declaration and reassignment is a good thing and therefore we must also avoid const. |
It doesn't have |
for what its worth, I wouldn't mind if variables in all caps became WHITESPACE = /^[^\n\S]+/ becomes const WHITESPACE = /^[^\n\S]+/; but I would not be inclined to see |
But it does generate "Var" in the bare JS it generates. It does not generate "const" even when I would like to. Sent from my mobile.
|
Maybe instead of that the CS compiler could error when trying to re-assign capitalized variables (kinda like what Ruby does): MESSAGE = 'hello'
foo = ->
MESSAGE = 'bye' # Error: cannot reassign MESSAGE constant. Same thing would apply to classes as they are usually declared using ThisNamingConvention. |
@dadleyy UPPERCASE is completely backwards incompatible (@epidemian O.o). I mean, I don't write that code, but it's a big world out there. I don't see the advantage of UPPERCASE over const. Wait, terseness... yummy yummy terseness... but wait... pinky abuse... (all torn up inside) |
The idea that you can use If I understood their motivation, ES6 was designed to allow new languages to be implemented more easily, and has features that mostly exist for that use case. Constants make more sense in that context, and shouldn't be used all over your codebase. IMO. |
Really the problem is that const can't be used at all in coffeescript which means it is taking a hard opinionated stance that it is a bad part, nay a terrible part of JS. Sent from my mobile.
|
Is there a good reason not to switch the 'var' ouput to 'const' and 'let' ? Perhaps with a flag - eg, --es6-vars and change UPPERCASE to const? Thoughts? |
The main reason is just a general aversion to compilation flags. They've always been rejected in the past. Still, they haven't been ruled out totally, and ES6 support will probably force the issue. It's difficult to see how CS classes can become compatible with JS classes without a breaking change, so CoffeeScript will almost certainly have to add compiler flags, or be replaced by a new dialect or language. |
A constant assignment operator has been discussed elsewhere, so you'd do something like: i = 0
pi := 3.141 A better looking operator would be nice, but it's a much less radical change at least. |
I get what you're saying, but I still think it's a better option than what we have right now. |
Can people please stop pushing this issue? |
Unless of course you use CS to write JS that will go into other ppls code. Then the lack of "let" "const" etc is a severe deficiency.
|
There was much debate about See also coffeescript6/discuss#31 and coffeescript6/discuss#35 |
Hi I'm using the examples from PragProg's "NodeJS done the right way".
Hit this problem they are using the --harmony flag to use ECMA6's features.
Figured out how to pass the flag to coffeescript using:
coffee --nodejs --harmony
This then throws an error:
for this line of code:
const fs = require('fs')
however this one executes correctly:
I believe the coffeescript parser is stopping on the 'const' keyword irrespective of the --harmony flag being passed.
The text was updated successfully, but these errors were encountered: