📜 ⬆️ ⬇️

ECMAScript Harmony and Node.js

I'll start from afar. From browsers. I’m not so often interested in new browser development, but then one day, out of boredom, I decided to see what javascript is coming and we’re able to offer modern browsers to offer us. This is, of course, not about ECMAScript 5, but about ECMAScript 6 ECMAScript Harmony . After a couple of minutes of surfing, I stumbled upon such a summary table . Not the most relevant - Chrome, for example, has long supported Proxy . Of course, the confusion was still the same. In the browser it is not realistic to use it yet.
In the browser. And on the server? On the server there is no need to support the zoo of various browsers. Node.js uses V8 - the same javascript engine as Chrome, respectively, it should support the same new language features.

Full of enthusiasm, I wrapped the testik with let and started the node.
- “And you FIG,” - said Noda - “syntax error”!
And, saddened, I briefly left this venture. As it turned out - in vain.
Node at a breakneck pace changing versions. Changes constantly wrote about changing versions of V8. And where is the long-awaited harmony?
A few days ago, another one came out, I buried myself in Google and almost immediately realized how foolish I was. It turns out that node.js has been supporting the launch flag --harmony and several others for a relatively long time:
flags
--harmony (enable all harmony features (except typeof))
--harmony_typeof (enable harmony semantics for typeof)
--harmony_scoping (enable harmony block scoping)
--harmony_modules (enable harmony modules (implies block scoping))
--harmony_proxies (enable harmony proxies)
--harmony_collections (enable harmony collections (sets, maps, and weak maps))
--use_strict (enforce strict mode) // a little off topic, but we need it

Launched. Tested. Works:)




So what do we have in ECMAScript Harmony Node.js at the moment?


So far not so much. There is no simple declaration of classes yet, you can not lick your lips. However, at least, due to Proxy , new features are worth paying attention to.
')

Constants


The only Harmony feature in V8 is enabled by default, without any flags, and many, I think, are constantly using it.
Earlier in js they wrote “constants” of caps, and some, in the same caps, in the hope that they would be listened to, left comments in the style:
var CONST_C=1;// !!!111 

Now everything is easier:
 const C=1; C=2; console.log(C);//=>1 

In strict mode, the scope of constants is similar to let , more on that later.

let and blocks


Works only in strict mode. If there is no desire to prescribe it permanently, run it with the --use_strict flag. However, only if only if not particularly actively using third-party libraries - many without, for example, arguments.callee or with cannot live.
let this is a new var , generally similar to it, except for one - the scope is limited to a block.
It looks like this:
 "use strict"; let i=1; { let i=2; console.log(i);//=>2; } console.log(i);//=>1; for(let i=0;i<5;i++) console.log(i);//=> 0, 1, 2, 3, 4 console.log(i);//=>1; if(true) var j=1;//OK if(true) let k=1;//!!!   - SyntaxError: Illegal let declaration in unprotected statement context. //,   : const C=1; { const C=2; console.log(C);//=>2; } console.log(C);//=>1; 


Proxy


Perhaps the most interesting of the opportunities available at the moment. Displays reflection in javascript to a new level. For example, allows you to dynamically create non-existent methods:
 var object=Proxy.create({ get:function(proxy,name){ return function(){ console.log(name); }; } }); object.test(); //=>"test" object.privet();//=>"privet" 

Once I doubted that such a thing would appear in javascript at all :)
Using Proxy.createFunction, you can do, for example, executable instances of classes and many more goodies.
Well painted here .

Set, Map and WeakMap


Set stores unique values, Map and WeakMap - associative arrays, where keys can be any objects and functions. In theory, WeakMap differs from Map in that when removing all references to an object representing a key, the key-value pair is also deleted from the array. However, in this implementation, V8, Map and WeakMap are the same, because it has not yet implemented an iterator.
 var st=new Set; st.add(5); st.add(5); console.log(st.has(5));//=>true st.delete(5); console.log(st.has(5));//=>false st.add(console); console.log(st.has(console));//=>true var map=new WeakMap; var obj={foo:"bar"}; map.set(obj,"val"); console.log(map.get(obj));//=>"val" map.delete(obj); console.log(map.get(obj));//=>undefined 


typeof null


 // console.log(typeof null);//=>"object" // console.log(typeof null);//=>"null" 

Here is such a controversial point. It may break the old code, so the --harmony flag does not include this feature. If you want to use it, run it with --harmony_typeof .

Modules


And now about the sad. Although there is a flag --harmony_modules and unit tests of the module system in the style of Harmony , I could not torment them. Attempting to use is either a segmentation fault or a syntax error. Who figured out - I will be grateful for the hint.



How to build in order to run without flags - didn’t figure it out either, and didn’t really try :) The rules in deps / v8 / src / flag-definitions.h , but never received anything except the segmentation fault . I would be grateful for the hint in this.

Before the approval of the specification is still far away, most of the tasty features ( tyk , tyk , tyk , tyk , tyk , etc.) are not yet implemented in V8, but, it seems, it is already quite good :)

Source: https://habr.com/ru/post/151690/


All Articles