📜 ⬆️ ⬇️

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

Good day, dear habraiser. I love everything new and beautiful and therefore very often I 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 now be started to test, so to speak, just for fun.

To help, you can take the latest version of Firefox, but I found a different way for myself. Next, we will discuss the new features of javascript, what awaits us about slimerjs .

So what is slimerjs? Probably worth getting to know him closer if we want to work with him. Essentially, slimerjs is nothing like phantomjs . This is a scripting browser for a developer that has an engine equivalent to the latest Firefox in its arsenal. This means that we have the full 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 Windows lover and I often come across the difficulty of tuning, but I don’t lose heart. I will describe all my actions for Windows, in fact, there is not much difficulty in setting up for another OS. And so it went.
')
First of all, we download slimerjs , unpack it into a folder, and set the system variables. In my case it was the folder C: / Tools / slimerjs, we add it to the path variable. We also need to set a variable for Firefox, since slimerjs requires Firefox to run. In my case, I set the SLIMERJSLAUNCHER variable like this: C: \ Program Files (x86) \ Mozilla Firefox \ firefox.exe. Well, this is where our magical manipulations end. Let's start writing the code to check the features of ecma 6.

The first thing I turned my attention to was the short recording of the function. Having found the use of the Internet in some detail, I decided to check it out. And about a miracle, amazement there was no limit. There are several options for recording, 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 by itself, because it is also a destructive operation. Let's check all these types of records. To do this, we need to create a file, let it be named ecma6-test-features.js and write the functions described above into it. We will add console.log to each function, and see the result by running the slimerjs ecma6-test-features.js command. At first, a window will open to us, of course, that it will disturb us, but it has a number of appointments that you can read about in the slimerjs documentation. In order to get rid of unwanted opening of the window, 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. By running our script, we get the result from all the above written functions. This recording of the functions I was very pleased.
What’s next in ecma6 is list comprehensions. What is list comprehensions is known to many, those who do not, I will briefly explain. This is a syntactic construct that serves to create lists using operations on existing lists. What will consider 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 happening here? The first expression is a function that is applied to each element of the array, the equivalent of which is map. The second entry is an iterator, for traversing the elements of an array. The third part, which contains if - is equivalent to filter. Given what was said, plus the possibility of a short function write, we will 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 record is familiar to many. Yes, yes, you were not mistaken so you can write in C #, Typescript And in other modern languages. Beautiful isn't it?

On this, while everything, then I wanted to tell and show (then put the source of all the tests) the rest of the possibilities of working with ecma6 now.

This is my first article. I had a lot of ideas to write, but I decided to start with this
I will be glad to hear comments and wishes for further improvement of myself as a writer of articles.

With respect to all. Until new meetings.

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


All Articles