Node.js es6 class support - javascript

Finally es6 classes have landed in Node.js v4.0.0. But the feature needs --use_strict option to be passed. e.g.
node --use_strict sampleClass.js
What does this --use_strict option signify? Has it anything to do with "use strict"; javascript directive.
Note: On Linux Classes worked in v0.12 too but not in Windows
Edit: If you want to omit --use_strict flag than use "use strict"; in js file

Well, JS has a strict mode which slightly changes how scoping works as well as other small things. Generally, it should be preferred since the bodies of classes and modules are always in strict mode anyway.
Classes work both in strict and loose mode. However, the JavaScript engine Node runs only supports running classes in strict mode at the moment.
This is a limitation of V8 (the engine) and will be resolved in the future. Here is the bug tracking it.

Starting from version 6, classes in NodeJS just work, so no --use_strict flag is required.

Related

How can those node modules which use native code be used in browser?

Some node modules like 'ursa' uses native code. I had a requirement to use it in browser so I bundled it with browserify but it did not do well. I got some errors which I found that was due to the fact that ursa usages native codes which was not found in the browser scope. So the question is how I can use modules like ursa in browser that uses natives.

Block-scoped declaration let const function class not yet supported outside strict mode in kriasoft react boiler plate

I am a starter in react and ES6. I started with the boilerplate "https://github.com/kriasoft/react-static-boilerplate"
As mentioned in the docs, i Did npm install->node run. But I am getting an error
as "Block-scoped declaration(let, const, hunciton, class) not yet supported outside strict mode."
I tried to put "use strict" at top of the run file, but shows another error "process.argv.includes is not a function"
I am using node version : 4.3.0, npm : 4.0.2. Does this cause the issue.
Do the version matter to support the es6 variables or is this any other issue.
Thanks.
node v4.x uses an older version of V8 (4.5) which did not fully support all scenarios for block-scoped variable declarations in sloppy (non-strict) mode. If you upgrade to a newer version of node, such as node v6.x, you won't have a problem because of the newer version of V8 used there (5.1).
Similarly, Array.prototype.includes was available behind a flag (--harmony_array_includes) in older versions of V8 (including the version used in node v4.x). However, since node v6.x, you can use it without a harmony flag.
For more ECMAScript compatibility information, check http://node.green.

Is it possible to run ES6 in Node REPL?

Is there any way to run ES6 in Node REPL (Read Evaluate Print Loop)? While running ES 6 commands, I am getting error as shown in the screenshot. Appreciate if someone can help me to configure Node to run ES6 code.
TL;DR: Upgrade Node
I have Node.js v6.0.0, which means I have all the ES6 features unlocked by default. My REPL has support for (basically) everything. Now, node v6.0.0 is currently in development, so you might not want to upgrade your production server, but if you're a developer, it's really stable enough for everyday use.
If you must use an outdated version of node, I would suggest you install n. It's a way to manage your versions of node on one machine.
Good Luck!
Node 4 and higher supports most ES6 features out of the box, here is compatibility table.
To use ES6 features in older Node versions, it should be started with --harmony flag, and code should run in strict mode.
It is not possible to enable strict mode with 'use strict' for REPL globally, so ES6 code should be placed inside IIFE.
Strict mode can be enabled globally with --use_strict. To enable experimental ES6 support in REPL in older Node versions (0.12.x and lower) it should be started with
node --harmony --use_strict
Add 'use strict'.
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
more information about use strict is here or here

Test Harmony features with Jasmine

What would be the best way to test Harmony features with Jasmine? Jasmine doesn't currently run my tests as it is puzzled by the yields and * symbols.
You'll have to manually run jasmine like this:
node --harmony ./node_modules/jasmine-node/bin/jasmine-node ./spec
You have to run your tests in an environment that supports Harmony features. Check out the ECMAScript 6 compatibility table for an overview of which features are supported in which browsers.
Even if your environment supports some of the new features, there's no guarantee that they conform to specifications. Make sure you read both the vendor documentation and the standard specification so you know what to expect.

ECMAScript:Harmony / ES6 to JavaScript compiler

After reading Peter's article on JavaScript I noticed
Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers.
There are currently two popular compilers with some vague ES:Harmony compliance:
Traceur
CoffeeScript
Although CoffeeScript has some compliance it's not designed to be an ES:Harmony compiler so it's not useful to this end.
Tracuer seems to be sticking more rigorously to the ES:Harmony specification but I don't know whether it intends to become a full ES:Harmony compiler.
Since the aim is to to compile ES6 down to ES3 it would also need to support ES5 features (and probably a switch whether to compile ES5 to ES3 or ES6 to ES3).
Are there currently any other projects aiming to create a full ES:Harmony to ES3 compiler?
Is it wise to start writing such a compiler knowing that the standard is young / unstable / in flux.
Are there currently any ES5 -> ES3 compilers?
I've left a question on the Traceur mailing list.
The aim of such a compiler would be backwards compatibility with ES3. Not full emulation of ES5 and ES6 in ES3.
(shameless but relevant plug below)
Caja is reworking its ES5 support via ES5/3 and will do the same for ES harmony. So our structure would be implemented as a Harmony to ES3 layer which can be skipped for real harmony implementations, and then a separable loader that preserves the security properties that concern caja.
Like Traceur, members of the Caja team are part of TC39 (the committee defining ES Harmony).
I don't know about Coffeescript's plans, but it was mentioned during discussions of Harmony modules. Module loaders will likely have the ability to intercept loaded source code (via eval hooks) and rewrite it before module initialization, so if a module is written in CoffeeScript, a runtime CoffeeScript rewriter could be invoked at initialization time. This would allow apps to be composed of modules written in multiple languages that compile down to Harmony at load time.
One thing to note is that not everything in Harmony can be implemented easily via translation. For example, implementing weak maps correctly would require implementing your own garbage collector in JavaScript and even if you did that you would probably just reintroduce the host object/native object cycle problem.
Check out TypeScript, Microsoft's new language based on ES6.
Continuum has implemented most of the relevant features and should run in es3 browsers (like older IEs).
Mascara is probably what you're looking for.
As of the time of typing, we now have Babel. It integrates with many different build tools/systems, and will transpile ES6+ in order to support legacy browsers (it doesn't state which version it targets, but it does say that it targets IE9+).
To install it type npm install babel -g.
Note that it has rather a lot of dependencies and when installed it is ~23.4 MB (2888 files).
Google Closure Compiler (Github) is a great tool for ES6 compilation. It's a simple Java jar that is used from the command line. There are other options such as API services and GUIs, but I find that it was best to set up an automatic build system hooking into the Java JAR. It can transpile your ES6 code into ES5 compatible code. I started using it for compressing and obfuscating code, but it can also do error checking and the ES6 transpilation as I mentioned.
Note that the ES6 features are marked as experimental. But I'm planning on using them in production soon, since my tests were rock solid.
There's also https://github.com/matthewrobb/six
Six is a language super-set of JavaScript that enables new syntactic features from the 6th edition of ECMAScript to be used, through a transpiler, in your scripts today.
WARNING: Still in a very early state, proceed with caution.
I'm not sure in what instance compilation back to ES3 would valuable as opposed to ES5, seeing that implementation changes are limited to array and object helper functions, and ES5 support is so prevalent.
So for completeness, another ES6 to ES5 compiler is the esnext project by Square. It is a collection of a number of modules designed to polyfill various ES6 features provided in one package. Here is the list modules included: https://github.com/square/esnext#available

Categories