Error in function arrow Unexpected token > [duplicate] - javascript

Is Node.js supporting => function keyword alias already? If yes, starting from which version? How to enable this language extension?
(function() { console.log('it works!') })()
Becomes
(() => { console.log('it works!') })()

In short: yes, arrow functions are reasonably well supported in Node.js since version 4.4.5.
Completely correct support starts with version 6. Initial support was introduced as far as v0.12 but is was very incomplete and disabled by default until v4.0 when it got better. See Node's ES6 compatibility table for details: http://node.green/#ES2015-functions-arrow-functions.

The syntax you're referring to is "arrow function" syntax. It is a feature of ECMAScript 6, also known as "Harmony". The ES6 standard is now finalized, but engines are still implementing its new features.
The V8 now has arrow function support. Node runs on the V8 engine, but it can take some time for Node to incorporate the latest version into its code base.
Whenever it is added, it might possibly be enabled only via a --harmony command-line flag.

You can follow this issue: https://code.google.com/p/v8/issues/detail?id=2700
Currently (as 02.05.2014) arrow functions have been implemented and waiting until this functionality will be landed in v8: https://codereview.chromium.org/160073006/
After then we'll need to wait, until v8 version with arrow function would be integrated into Node.JS. You can follow Node.JS changelog there: https://github.com/joyent/node/blob/master/ChangeLog (search for "v8: upgrade to ....")

kangax's compatibility tables can keep you up-to-date with what is currently available in Node.
Experimental features can be enabled using the instructions on this page:
All shipping features are turned on by default on Node.js
Staged feature require a runtime flag: --es_staging (or its synonym, --harmony)
In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring) but this is highly discouraged

Related

ES6 (ECMAScript 2015) support as of 2018

As a long time programmer just getting into JavaScript programming, I have the following questions that are still unclear despite having read many articles.
Looking at the ES6 (ECMAScript 2015) support by browsers, I can see that the supporting level is much less than that of Node.js, so the question is,
If both Node.js and browsers are using the modern V8 engine, why supporting level are so different?
Looking at the ES6 support in Node.js, I can see really really few ES6 features are unsupported now. However, what exactly does the supported means in the chart? I.e.,
Does it means even I write using the support ES6 features, I still need to use the Babel compiler to compile ES6 code to ES5 for Node.js to use it?
For TypeScript ES6-style JavaScript code that runs for Node.js, they are still need to be transpiled into an ES5 compatible form, despite that Node.js almost cover all ES6 featues, right? I.e.,
for the following code,
class Animal {
constructor(public name) { }
move(meters) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
move() {
console.log("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
move() {
console.log("Galloping...");
super.move(45);
}
var sam = new Snake("Sammy the Python")
var tom: Animal = new Horse("Tommy the Palomino")
sam.move()
tom.move(34)
Does it need to be transpiled into an ES5 compatible form to runs with Node.js or not?
Finally, any online site that I can try playing with TypeScript/ES6 code like above?
I copy it to my chrome console, and got an error that I don't understand - Unexpected strict mode reserved word, and
I tried it on http://www.typescriptlang.org/play/index.html, but the console output is not working there.
Please help. thx.
Looking at the ES6 (ECMAScript 2015) support by browsers, I can see that the supporting level is much less than that of Node.js, so the question is,
Many different browsers and many different Javascript engines in them, each with their own level of ES6 support. The latest version of node.js is generally pretty up-to-date on what the V8 engine supports. Many browsers have longer release cycles and may not be as current, but each is different and has their own release strategy and level of ES6 support.
If both Node.js and browsers are using the modern V8 engine, why supporting level are so different?
If you compare the latest release of node.js with the latest release of Chrome on Windows, you won't see much difference in support. The ES6 support chart you're looking at seems old to me. For example, Chrome has had support for the Set object for a long time, but your chart says false.
Looking at the ES6 support in Node.js, I can see really really few ES6 features are unsupported now. However, what exactly does the supported means in the chart? I.e.,
Supported means you can use the feature directly without a transpiler, but how accurate that is depends upon the source of the document claiming it. Some documents do extensive testing of all the various edge cases of a given feature.
Others just look for general implementation. So if for example, you're looking at support for the Set object and it says "supported", then that is suppose to mean that you can just write plain Javascript that uses the Set object and it will just work. How accurate that document is depends upon the source of their data and the thoroughness of their testing.
Does it means even I write using the support ES6 features, I still need to use the Babel compiler to compile ES6 code to ES5 for Node.js to use it?
No. In a Javascript engine that supports a given feature in ES6, you can write ES6 code for that feature and directly run it in that Javascript engine. No transpiling is needed.
For TypeScript ES6-style JavaScript code that runs for Node.js, they are still need to be transpiled into an ES5 compatible form, despite that Node.js almost cover all ES6 featues, right? I.e.,
The class definitions you show are plain ES6 code. Those will work just fine as is in an ES6 capable Javascript engine.
If you write Typescript code, then you will have to transpile the TypeScript to Javascript because no Javascript engine (I know of) supports TypeScript directly. When transpiling form TypeScript to Javascript, you can usually specify whether you want the transpiler to generate ES5 compatible code (which will run in an ES5 engine or an ES6 engine) or ES6 compatible code (which will only run in an ES6 engine) depending upon what your target environment is capable of.
Does it need to be transpiled into an ES5 compatible form to runs with Node.js or not?
Your particular code appears to contain at least one TypeScript style variable declaration which would need to be transpiled. The rest looks like plain ES6 Javascript which should work in any ES6 engine without transpiling.
When I remove the TypeScript, fix some syntax errors in your code and implement the Animal constructor properly, then this code works fine in node.js v8.8.1 (which is what I currently have installed) and in Chrome 63.0.3239.132, Edge 41.16299.15.0 and Firefox 57.0.4 all on Windows 10:
// Generic ES6 code
class Animal {
constructor(name) {
this.name = name;
}
move(meters) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
move() {
console.log("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
move() {
console.log("Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python");
var tom = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
You can run this snippet yourself in any browser you desired to see the results (assuming the browser is modern enough to support a stack overflow snipppet). It works in all the current versions of browsers I have except IE 11.192.16299.0 (no surprise that IE doesn't support ES6).
I copy it to my chrome console, and got an error that I don't understand - Unexpected strict mode reserved word,
This happened to me when I tried to run your code in node.js until I removed the TypeScript from it so that it was just plain ES6. I think this particular error is caused by the public in this line:
constructor(public name) { }
since that is not part of the ES6 specification (it's apparently part of TypeScript).
It seems that there's one question you're dying to ask, but haven't exactly articulated is: "How do you know whether you have to transpile or not?".
The answer is that you have to understand the cross between the target environments you wish to run in and the newest features you plan to use. If you are writing server-side code that will only run in node.js, then it's a lot simpler. Examine a comprehensive table such as http://node.green/, study what it says for the node.js version you plan to use and the feature in question. If it indicates you should be able to use that feature, then write your code using that feature, write a test case for it and verify that both the code you wrote and the feature you are using both work. Add that to your body of knowledge about what you can and can't use in that version of node.js. You can then assume all future versions of node.js will also support that feature.
If you're writing code to run in a browser, life is much more complicated. If you plan to support a lot of browsers and really don't want to worry about ES6 support at all, then just transpile to an ES5 target and go about your business.
If you want to use non-transpiled code, then you have a lot of testing to do in a lot of browsers. You have to first specify exactly which versions of which browsers you are going to support and then you have to write your code and test cases and you have to test in every browser you plan to support. There really is no shortcut. When you find things that don't work, you'll have to either look for polyfills or work-arounds or stop using that ES6 feature.
Test the code in the environments that the code is going to be used in. Use the available means to implement the specific standard or specification within the environment that you are using the code at. Or try to create an approach yourself to resolve an issue that you encounter during development of your code while noting the progressions and persistent issue for others to be able to possibly address and resolve the issue, bug or requirement from their own perspective.
Simply due to the fact the a document states that the browser has implemented a specification or standard does not mean that the implementation is consistent with the specification, or implemented at all. The only way to verify whether a browser implements a standard is to test with code yourself. File issues and attempt to fix bugs yourself.
Browsers use different engines including Gecko, WebKit, not V8 alone; and can change over time in both name and implementations of specifications; see Monitor and potentially deprecate support for multitrack SourceBuffer support of 'sequence' AppendMode; How to use "segments" mode at SourceBuffer of MediaSource to render same result at Chomium, Chorme and Firefox?. There are many browsers. For example, Lynx does not use V8.
See web platform tests
The web-platform-tests Project is a W3C-coordinated attempt to build a
cross-browser testsuite for the Web-platform stack. Writing tests in a
way that allows them to be run in all browsers gives browser projects
confidence that they are shipping software that is compatible with
other implementations, and that later implementations will be
compatible with their implementations. This in turn gives Web
authors/developers confidence that they can actually rely on the Web
platform to deliver on the promise of working across browsers and
devices without needing extra layers of abstraction to paper over the
gaps left by specification editors and implementors.
For example, one test for Web Speech API, where volume property is specified as capable of being set, though was not able to detect a change of audio output for either Chromium or Firefox when setting the volume property of SpeechSynthesisUtterance to different values within the specified ranges.
Specifications are a totally different regime than actual browser implementations. Specifications or standards can be and are written well in advance of actual browser implementation, if implemented at all. You can use browserify, or write the code yourself to use NodeJS modules or other non-native code in the browser.

es6 modules native support

I understand the ES6 modules specification, the question is about its support. AFAIK, there are no browsers that implement this natively (see Browser compatibility here). Got a couple of questions about this:
ES6 modules is an ES6 feature, obviously. When I look at kangax compatibility table, I don't see such row (for ES6 modules) at all, why is that? It's a ES6 feature afterall...
hence, the only way to use ES6 right now is to use a build tool, such as babel, browserify or any other aternative, right?
how are the modules gonna be fetched, when they're natively supported - as async AJAX calls from the browser?
ES6 modules is an ES6 feature, obviously. When I look at kangax compatibility table, I don't see such row (for ES6 modules) at all, why is that?
There's an issue being discussed on kangax's github.
the only way to use ES6 right now is to use a build tool, such as babel, browserify or any other aternative, right?
No. There is also a polyfill by Guy Bedford at http://github.com/ModuleLoader/browser-es-module-loader.
And there's already the preview release of Edge that implements it natively.
how are the modules gonna be fetched, when they're natively supported - as async AJAX calls from the browser?
Yes but not exactly: they will be fetched when the are used for the first time, by a call to an object (a function or a property) on the imported library.
Native modules are available in the following versions of browsers:
Safari 10.1
Chrome Canary 60 – behind the Experimental Web Platform
flag in chrome:flags.
Firefox 54 – behind the dom.moduleScripts.enabled setting in about:config.
Edge 15 – behind
the Experimental JavaScript Features setting in about:flags.
source

Cannot get ES6 functions in Node 0.11.13 running --harmony flag [duplicate]

Is Node.js supporting => function keyword alias already? If yes, starting from which version? How to enable this language extension?
(function() { console.log('it works!') })()
Becomes
(() => { console.log('it works!') })()
In short: yes, arrow functions are reasonably well supported in Node.js since version 4.4.5.
Completely correct support starts with version 6. Initial support was introduced as far as v0.12 but is was very incomplete and disabled by default until v4.0 when it got better. See Node's ES6 compatibility table for details: http://node.green/#ES2015-functions-arrow-functions.
The syntax you're referring to is "arrow function" syntax. It is a feature of ECMAScript 6, also known as "Harmony". The ES6 standard is now finalized, but engines are still implementing its new features.
The V8 now has arrow function support. Node runs on the V8 engine, but it can take some time for Node to incorporate the latest version into its code base.
Whenever it is added, it might possibly be enabled only via a --harmony command-line flag.
You can follow this issue: https://code.google.com/p/v8/issues/detail?id=2700
Currently (as 02.05.2014) arrow functions have been implemented and waiting until this functionality will be landed in v8: https://codereview.chromium.org/160073006/
After then we'll need to wait, until v8 version with arrow function would be integrated into Node.JS. You can follow Node.JS changelog there: https://github.com/joyent/node/blob/master/ChangeLog (search for "v8: upgrade to ....")
kangax's compatibility tables can keep you up-to-date with what is currently available in Node.
Experimental features can be enabled using the instructions on this page:
All shipping features are turned on by default on Node.js
Staged feature require a runtime flag: --es_staging (or its synonym, --harmony)
In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring) but this is highly discouraged

Ecmascript 6 support on Node.js

I've been working with KoaJS for a while, and we can easily use the 'let' keyword and the generators when using the --harmony flag but I couldn't find how much support for does the node v0.11.x provides while using the same.
I tried using the default value argument initialization but couldn't succeed.
Is there any source available which can list the no of features of ECS 6 supported in node v0.11.x using the harmony flag? Or if there is any npm module available for node that might allow me to use the same?
Thanks in advance.
With regards to your second question, yes, there is es6-module-loader.
For a long list of transpilers, shims, and other tools for using full ES6 features now, see addyosmani's ECMAScript 6 Tools page.
As for native ES6 support in node.js, V8 officially implements "ECMAScript" but AFAIK the V8 project doesn't release a spec of their implementation.
However there are some sources of useful information out there.
Here's a brief overview of ES6 in node.js v0.11.6.
You may want to determine the version of V8 that your version of node.js uses.
See the node.js blog for recent changelog info.
It can also be useful to find the version of V8 used in a given Chromium release.
The Chrome release notes can be found here.
Keep in mind that different flags can be set for the same version of V8.
Chromium and node.js both have ways to set flags in V8 related to ES6 support.
Here are two tables that list ES(6) feature support across implementations:
http://pointedears.de/scripts/test/es-matrix/
http://kangax.github.io/compat-table/es6/
This MDN page lists a set of reference articles for ES6 language features.
At the bottom of each one you can see the status of Chrome support for that feature (and using V8 versions determine the support in node.js).
Finally, the V8 issue tracker
provides a list of issues related to ES6 features, many of which have been implemented and their issues closed.
You can use ~96% of ES6 features in Node.js 6. You can review support for all versions on http://node.green/
This does not concern node 0.11, but in the current 5.8.0, you can use --harmony_default_parameter.
It it scheduled to be included by default in v6.0.

Node.js support for => (arrow function)

Is Node.js supporting => function keyword alias already? If yes, starting from which version? How to enable this language extension?
(function() { console.log('it works!') })()
Becomes
(() => { console.log('it works!') })()
In short: yes, arrow functions are reasonably well supported in Node.js since version 4.4.5.
Completely correct support starts with version 6. Initial support was introduced as far as v0.12 but is was very incomplete and disabled by default until v4.0 when it got better. See Node's ES6 compatibility table for details: http://node.green/#ES2015-functions-arrow-functions.
The syntax you're referring to is "arrow function" syntax. It is a feature of ECMAScript 6, also known as "Harmony". The ES6 standard is now finalized, but engines are still implementing its new features.
The V8 now has arrow function support. Node runs on the V8 engine, but it can take some time for Node to incorporate the latest version into its code base.
Whenever it is added, it might possibly be enabled only via a --harmony command-line flag.
You can follow this issue: https://code.google.com/p/v8/issues/detail?id=2700
Currently (as 02.05.2014) arrow functions have been implemented and waiting until this functionality will be landed in v8: https://codereview.chromium.org/160073006/
After then we'll need to wait, until v8 version with arrow function would be integrated into Node.JS. You can follow Node.JS changelog there: https://github.com/joyent/node/blob/master/ChangeLog (search for "v8: upgrade to ....")
kangax's compatibility tables can keep you up-to-date with what is currently available in Node.
Experimental features can be enabled using the instructions on this page:
All shipping features are turned on by default on Node.js
Staged feature require a runtime flag: --es_staging (or its synonym, --harmony)
In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring) but this is highly discouraged

Categories