Add ECMA6 class support in NodeJS [duplicate] - javascript

A new stable release of Node.js (0.12) has landed recently with an upgraded Google's v8 JavaScript engine, v3.28.73.
What ECMAScript 6 features are currently present in Node.js, without using the --harmony flag?
I have checked several sites claiming to list the ES 6 features but all of them seem out of date - most prominently, this table (Update: now updated with current Node.js status as of 0.12), because several of the features are listed as requiring the --harmony flag while I found some of them being enabled by default (Maps, Sets, Symbols, to name a few). Update: Node specific tables have since been made available
Also, trying to google this information purely for the v8 engine gives too up-to-date information - current v8 release is 4.2.*, which is quite ahead of what Node.js uses.
My hopes are that this question (and its answers) will become a comprehensive summary on what ES 6 features are now available to Node.js developers.
ES 6 features enabled in Node.js 0.12 I currently know of:
Maps, Sets / WeakMaps, WeakSets
Symbols
Object.observe
Promises
Number
.isInteger
.isSafeInteger
.isNaN
.EPSILON
.MIN_SAFE_INTEGER
.MAX_SAFE_INTEGER
Math
.clz32
.imul
.sign
.log10
.log2
.log1p
.expm1
.cosh
.sinh
.tanh
.acosh
.asinh
.atanh
.trunc
.fround
.cbrt
.hypot

Features without --harmony flag:
"for-of" loop
Map, Set, WeakMap, WeakSet (already specified in question)
Symbol (already specified in question)
Promise (already specified in question)
Array methods:
.keys()
.values()
.entries()
[Symbol.iterator]
Object:
.observe() (initially was planned for ES7, but was removed from the spec entirely on November 2, 2015)
.is()
.setPrototypeOf()
.getOwnPropertySymbols()
.getNotifier() (not es6, example here)
.apply() and .call() (not es6, same purpose as Funciton.prototype.call and Function.prototype.apply)
Number properties and methods (already specified in question)
.isInteger()
.isSafeInteger()
.isNaN()
.isFinite()
EPSILON
MIN_SAFE_INTEGER
MAX_SAFE_INTEGER
Math methods (a lot of them) (already specified in question)
constants
I thinks that's all that we have without --harmony flag.
Features with --harmony flag:
generators
arrow functions (without need of --harmony_arrow_functions flag in contrast to io.js)
let variables - only in strict mode
Binary and octal literals
String methods:
.contains() (was replaced by includes() in actual ES6 specification)
.startsWith()
.endsWith()
.codePointAt()
.repeat()
.normalize()
String.fromCodePoint
Proxy (behind the --harmony-proxies flag)
I think that's all. Maybe if I forgot something - I'll add it later to the list.

ES6 features trickle down to Node in phases. Node uses Google's V8 as the JavaScript engine. A feature being supported in Node means it first has to be implemented in V8 and then Node team has to incorporate it in Node.js.
The team at Google releases a new version of V8 roughly every six weeks, and then it's up to the Node team to take it into use.
Manually curated lists of language features are nice but can become outdated quickly. Node 0.12 is not that in flux anymore, but typically manually curated list becomse obsolete as soon as a new version of Node is rolled out.
Here are two alternate ways to check what features a Node version supports, without relying on a static list. For further reading and more detailed examples of using these, you can check "How to check if Node.js supports ES 6 language feature"
#1 Easy - compatibility table
A dynamically generated list that relies on small tests to confirm the presence of a language feature stays better up to date. One such popular list is kangax.github.io/compat-table/es6/. We are interested only in Node features, so you can use
http://node.green
that leverages the same data as the kangax site.
#2 Hard - backtrack V8 version
Node uses V8 engine, so determining which version of V8 is included in Node tells us what ES6 language features are supported. You can find out which version of V8 was bundled in Node with node -p process.versions.v8.
$ node -p process.versions.v8
4.6.85.31
Then using Google's V8 project resources you can find which features are implemented in each version. The V8 project keeps an issue tracker where you can find ES6+beyond features marked with the harmony label.

Related

Which version of ECMAScript does the Google Apps Script V8 Runtime use?

When you crate a new Google Apps Script, it seems to support the v8 runtime by default. The documentation states:
Apps Script supports two JavaScript runtimes: the modern V8 runtime and an older one powered by Mozilla's Rhino JavaScript interpreter.
The V8 runtime supports modern ECMAScript syntax and features.
The V8 runtime documentation states:
You can use modern ECMAScript syntax in scripts that are powered by the V8 runtime. This syntax includes let, const, and many other popular features.
In both cases, they are very vague as to which ECMAScript version is supported, simply stating "modern ECMAScript syntax". This is problematic because there are 7 versions that were released between 2015 and 2021. Thus "modern" could refer to any one of these versions.
For example, I could easily assume that "modern" refers to the latest, 12th edition (2022) of ECMAScript, and end up writing code like this:
let a = 1_000;
However, attempting to use that syntax leads to the error:
Syntax error: ParseError: Unexpected token ILLEGAL line: ...
Rather than manually go through each of the remaining 6 versions until I find the latest one supported, it would be great to find documentation that explicitly states which ECMAScript version is supported.
Note: The previous related question (Which Edition of ECMA-262 Does Google Apps Script Support?) is not helpful since those answers also refer to "modern ECMAScript" rather than a definitive, specific version.
Which version of ECMAScript is supported by the V8 runtime?
There is some nuance here:
Which version of V8 does Google Apps Script use?
A reasonably recent version, and it gets updated every so often. I believe the idea is to track or slightly lag behind stable Chrome releases, but (as with any large project updating its dependencies) there may occasionally be hiccups/delays. Right now it should be somewhere in the 9.x version range. (For future readers: I expect this statement to be outdated before 2022 is over!)
Which version of ECMAScript does the Google Apps Script V8 Runtime Support?
I suppose if there was a simple answer to this, you'd find that in the documentation. As #Kaiido said in comments, JavaScript engines implement new JavaScript features one by one (rather than EcmaScript versions). So, for browsers just like for environments like GAS, it usually makes more sense to ask "is feature X supported?", because it may well be that some, say, ES2020 features are still missing but some ES2021 features are already available.
Why does let a = 1_000; produce a Syntax Error?
Well, the V8 version that GAS uses is sufficiently new (by at least two years) to support it; but the overall GAS experience depends on more than V8: the editor is parsing the entered source in order to provide help or highlighting or error checking or whatnot. It looks like the GAS team is aware that certain features aren't supported yet by the components responsible for that, and is actively working to remedy that. (I have no idea what the timeline is.)
Why does let a = 1_000; produce a Syntax Error?
Just to expand on #jmrk's answer about new features not supported by the parser.
function test2564(){
//let a = 1_000; throws syntax error by the parser
console.info(eval(`1_000`));// correctly logs 1000
}
The underlying V8 engine is good and supports the latest features, but the parser won't allow you to save or execute the project with those features, as it considers them as syntax errors.

Do Classes and template strings work in Node 0.12.7 in harmony mode [duplicate]

A new stable release of Node.js (0.12) has landed recently with an upgraded Google's v8 JavaScript engine, v3.28.73.
What ECMAScript 6 features are currently present in Node.js, without using the --harmony flag?
I have checked several sites claiming to list the ES 6 features but all of them seem out of date - most prominently, this table (Update: now updated with current Node.js status as of 0.12), because several of the features are listed as requiring the --harmony flag while I found some of them being enabled by default (Maps, Sets, Symbols, to name a few). Update: Node specific tables have since been made available
Also, trying to google this information purely for the v8 engine gives too up-to-date information - current v8 release is 4.2.*, which is quite ahead of what Node.js uses.
My hopes are that this question (and its answers) will become a comprehensive summary on what ES 6 features are now available to Node.js developers.
ES 6 features enabled in Node.js 0.12 I currently know of:
Maps, Sets / WeakMaps, WeakSets
Symbols
Object.observe
Promises
Number
.isInteger
.isSafeInteger
.isNaN
.EPSILON
.MIN_SAFE_INTEGER
.MAX_SAFE_INTEGER
Math
.clz32
.imul
.sign
.log10
.log2
.log1p
.expm1
.cosh
.sinh
.tanh
.acosh
.asinh
.atanh
.trunc
.fround
.cbrt
.hypot
Features without --harmony flag:
"for-of" loop
Map, Set, WeakMap, WeakSet (already specified in question)
Symbol (already specified in question)
Promise (already specified in question)
Array methods:
.keys()
.values()
.entries()
[Symbol.iterator]
Object:
.observe() (initially was planned for ES7, but was removed from the spec entirely on November 2, 2015)
.is()
.setPrototypeOf()
.getOwnPropertySymbols()
.getNotifier() (not es6, example here)
.apply() and .call() (not es6, same purpose as Funciton.prototype.call and Function.prototype.apply)
Number properties and methods (already specified in question)
.isInteger()
.isSafeInteger()
.isNaN()
.isFinite()
EPSILON
MIN_SAFE_INTEGER
MAX_SAFE_INTEGER
Math methods (a lot of them) (already specified in question)
constants
I thinks that's all that we have without --harmony flag.
Features with --harmony flag:
generators
arrow functions (without need of --harmony_arrow_functions flag in contrast to io.js)
let variables - only in strict mode
Binary and octal literals
String methods:
.contains() (was replaced by includes() in actual ES6 specification)
.startsWith()
.endsWith()
.codePointAt()
.repeat()
.normalize()
String.fromCodePoint
Proxy (behind the --harmony-proxies flag)
I think that's all. Maybe if I forgot something - I'll add it later to the list.
ES6 features trickle down to Node in phases. Node uses Google's V8 as the JavaScript engine. A feature being supported in Node means it first has to be implemented in V8 and then Node team has to incorporate it in Node.js.
The team at Google releases a new version of V8 roughly every six weeks, and then it's up to the Node team to take it into use.
Manually curated lists of language features are nice but can become outdated quickly. Node 0.12 is not that in flux anymore, but typically manually curated list becomse obsolete as soon as a new version of Node is rolled out.
Here are two alternate ways to check what features a Node version supports, without relying on a static list. For further reading and more detailed examples of using these, you can check "How to check if Node.js supports ES 6 language feature"
#1 Easy - compatibility table
A dynamically generated list that relies on small tests to confirm the presence of a language feature stays better up to date. One such popular list is kangax.github.io/compat-table/es6/. We are interested only in Node features, so you can use
http://node.green
that leverages the same data as the kangax site.
#2 Hard - backtrack V8 version
Node uses V8 engine, so determining which version of V8 is included in Node tells us what ES6 language features are supported. You can find out which version of V8 was bundled in Node with node -p process.versions.v8.
$ node -p process.versions.v8
4.6.85.31
Then using Google's V8 project resources you can find which features are implemented in each version. The V8 project keeps an issue tracker where you can find ES6+beyond features marked with the harmony label.

ECMAScript 6 features available in Node.js 0.12

A new stable release of Node.js (0.12) has landed recently with an upgraded Google's v8 JavaScript engine, v3.28.73.
What ECMAScript 6 features are currently present in Node.js, without using the --harmony flag?
I have checked several sites claiming to list the ES 6 features but all of them seem out of date - most prominently, this table (Update: now updated with current Node.js status as of 0.12), because several of the features are listed as requiring the --harmony flag while I found some of them being enabled by default (Maps, Sets, Symbols, to name a few). Update: Node specific tables have since been made available
Also, trying to google this information purely for the v8 engine gives too up-to-date information - current v8 release is 4.2.*, which is quite ahead of what Node.js uses.
My hopes are that this question (and its answers) will become a comprehensive summary on what ES 6 features are now available to Node.js developers.
ES 6 features enabled in Node.js 0.12 I currently know of:
Maps, Sets / WeakMaps, WeakSets
Symbols
Object.observe
Promises
Number
.isInteger
.isSafeInteger
.isNaN
.EPSILON
.MIN_SAFE_INTEGER
.MAX_SAFE_INTEGER
Math
.clz32
.imul
.sign
.log10
.log2
.log1p
.expm1
.cosh
.sinh
.tanh
.acosh
.asinh
.atanh
.trunc
.fround
.cbrt
.hypot
Features without --harmony flag:
"for-of" loop
Map, Set, WeakMap, WeakSet (already specified in question)
Symbol (already specified in question)
Promise (already specified in question)
Array methods:
.keys()
.values()
.entries()
[Symbol.iterator]
Object:
.observe() (initially was planned for ES7, but was removed from the spec entirely on November 2, 2015)
.is()
.setPrototypeOf()
.getOwnPropertySymbols()
.getNotifier() (not es6, example here)
.apply() and .call() (not es6, same purpose as Funciton.prototype.call and Function.prototype.apply)
Number properties and methods (already specified in question)
.isInteger()
.isSafeInteger()
.isNaN()
.isFinite()
EPSILON
MIN_SAFE_INTEGER
MAX_SAFE_INTEGER
Math methods (a lot of them) (already specified in question)
constants
I thinks that's all that we have without --harmony flag.
Features with --harmony flag:
generators
arrow functions (without need of --harmony_arrow_functions flag in contrast to io.js)
let variables - only in strict mode
Binary and octal literals
String methods:
.contains() (was replaced by includes() in actual ES6 specification)
.startsWith()
.endsWith()
.codePointAt()
.repeat()
.normalize()
String.fromCodePoint
Proxy (behind the --harmony-proxies flag)
I think that's all. Maybe if I forgot something - I'll add it later to the list.
ES6 features trickle down to Node in phases. Node uses Google's V8 as the JavaScript engine. A feature being supported in Node means it first has to be implemented in V8 and then Node team has to incorporate it in Node.js.
The team at Google releases a new version of V8 roughly every six weeks, and then it's up to the Node team to take it into use.
Manually curated lists of language features are nice but can become outdated quickly. Node 0.12 is not that in flux anymore, but typically manually curated list becomse obsolete as soon as a new version of Node is rolled out.
Here are two alternate ways to check what features a Node version supports, without relying on a static list. For further reading and more detailed examples of using these, you can check "How to check if Node.js supports ES 6 language feature"
#1 Easy - compatibility table
A dynamically generated list that relies on small tests to confirm the presence of a language feature stays better up to date. One such popular list is kangax.github.io/compat-table/es6/. We are interested only in Node features, so you can use
http://node.green
that leverages the same data as the kangax site.
#2 Hard - backtrack V8 version
Node uses V8 engine, so determining which version of V8 is included in Node tells us what ES6 language features are supported. You can find out which version of V8 was bundled in Node with node -p process.versions.v8.
$ node -p process.versions.v8
4.6.85.31
Then using Google's V8 project resources you can find which features are implemented in each version. The V8 project keeps an issue tracker where you can find ES6+beyond features marked with the harmony label.

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.

ECMAScript 6 modules in Node.JS

Node.JS v0.11.3 claims to have support for ECMAScript 6 modules with the flag --harmony_modules.
I have tried various examples, such as the following.
module math {
export var pi = 3.141593;
}
What is the syntax to get modules working in Node.JS?
The modules implementation in V8 is incomplete. There's parsing support when enabled with --harmony-modules, but support of the actual functionality was put on hold. The reason for this is because the specification for how ES6 modules will actually work has been in the works and is still not fully nailed down.
The implementation in Continuum (the linked screenshot from Crazy Train's answer) dates back to an interim spec from November 2012 and is now woefully out of date because of the ongoing changes to the ES6 module's spec. This is why the V8 devs put development of support for modules on hold.
It seems like the modules spec is approaching stability (though I expect we'll see small refinements for a while) and I think (hope at least) that we'll see SpiderMonkey and V8 moving forward with implementations over the next 6 months.
Useful links:
V8 modules bug: https://code.google.com/p/v8/issues/detail?id=1569
SpiderMonkey modules bug: https://bugzilla.mozilla.org/show_bug.cgi?id=harmony%3Amodules
You can use Continuum, which is an ES6 virtual machine written in (current) JavaScript.

Categories