How to take over the world, or javascript next now (part one)

Good day dear habrayuzer. I love everything new and beautiful and therefore I often look at the development of ecma 6 aka Harmony. Yes, yes, you were not mistaken, it will be about the new javascript, although it is still in development, but many features can already be started to test, so to speak, just for your pleasure.

You can use the latest version of Firefox to help, but I found a more different way for myself. Next, we will talk about the new features of javascript, which awaits us and slimerjs .

So what exactly is slimerjs? Probably worth getting to know him better if we want to work with him. Essentially slimerjs are nothing but phantomjs. This is a scripting browser for the developer with in its arsenal an engine equivalent to the latest Firefox. This means that we have every opportunity to use it as a REPL.

But let's leave the theory for the future and consider how to work with slimerjs. Since I am a fan of Windows, and often encounter the difficulty of settings, but I'm not discouraged. I will describe all my actions for Windows, as a matter of fact there is no special difficulty to configure for another OS. And so let's go.

First of all, we download slimerjs , unpack it into a folder, and prescribe system variables. In my case, it was the C: / Tools / slimerjs folder, add this to the path variable. We also need to set a variable for Firefox, since slimerjs requires Firefox to run. Variable SLIMERJSLAUNCHERin my case, I installed it like this: C: \ Program Files (x86) \ Mozilla Firefox \ firefox.exe. Well, this is where our magical manipulations end. Let's start writing code to check ecma 6 features.

The first thing I turned my attention to was a short record of the function. Having found in the Internet , a fairly detailed description of the use, I decided to check it. And lo and behold, amazement knew no bounds. There are several recording options, I will give everything.

1) var square = function(x) { return x * x; }
2) var square2 = function(x) x * x;
3) var square3 = x => { return x * x };
4) var square4 = x => x * x;
5) var squareAndCube = x => [x * x, x * x * x];

The fourth option is familiar to many developers working with C #, Scala, Typescript. This is nothing more than a lambda operator.
The fifth option is interesting in itself, in that it is also a destructive operation. Let's check all these record types. To do this, we need to create a file, let it be called ecma6-test-features.js and write the functions described above into it. Assign console.log to each function, and see the result by running the slimerjs ecma6-test-features.js command. At first, a window will open for us, of course, that it will interfere with us, but it has a number of purposes, which can be found in the slimerjs documentation. In order to get rid of unwanted window opening, at the end of our script file you need to add such code slimer.exit (). Now we have a console similar to nodejs in which we can execute our code. Running our script, we get the result from all the above written functions. Such a recording of functions made me very happy.
What's next in line in ecma6 is list comprehensions. What is list comprehensions known to many, those who do not, I will briefly explain. This is such a syntax construct that serves to create lists by applying operations on existing lists. So let's look at an example to understand how it is.

var list = [1,2,3,4,5];
var newList = [x * x for(x of list) if(x % 2 === 0)];

So what is going on here? The first expression is a function that applies to each element of the array, the equivalent of which is map. The second record is an iterator, for passing through the elements of the array. The third part, which contains if, is the equivalent of filter. Given what was said, plus the possibility of a short write function, we write this piece of code in a more familiar form.

var list = [1,2,3,4,5];
var newList = list.filter(x => x % 2 === 0).map(n => n * n);

This entry is familiar to many. Yes, you were not mistaken, you can write in C #, Typescript, and in other modern languages. Nice isn't it?

That's all for now, then I wanted to tell and show (then lay out the sources of all tests) the rest of the possibilities of working with ecma6 right now.

This is my first article. I had a lot of ideas on what to write, but I decided to start with this.
I will be glad to hear comments and suggestions in order to further improve myself as an article writer.

Respectfully to all. See you again.

Also popular now: