|
Two days ago, Node V4 has been officially released. This is after it merged with io.js released the first stable version, many new highlights, added ES6. Has a lot of the ES6 overview, this article we will explain how to use them.
1. Template Strings (String template)
If you've ever tried to create a multi-line JavaScript string, you might be doing:
var message = [
'The quick brown fox',
'Jumps over',
'The lazy dog'
] .join ( '\ N')
This applies only to a small amount of text, more time when the sentence will be very confusing. Therefore, a smart developers came up with a multiline below this is called the hack:
var multiline = require ( 'multiline');
var message = multiline (function () {/ *
The quick brown fox
jumps over
the lazy dog
* /});
Fortunately, ES6 brought us the template string:
var message = `
The quick brown fox
jumps over
the lazy dog
`;
In addition, it gives us a string interpolation:
var name = 'Schroedinger';
// Stop doing this ...
var message = 'Hello' + name + ', how is your cat?';
var message = [ 'Hello', name, ', how is your cat?'] join ( '').;
. Var message = require ( 'util') format ( 'Hello% s, how is your cat?', Name);
// And instead do that ...
var message = `Hello $ {name}, how is your cat`?;
See details on the template string on MDN
2. Classes (class)
ES5 defined class looks a little strange, and it takes some time:
var Pet = function (name) {
this._name = name;
};
Pet.prototype.sayHello = function () {
console.log ( '* scratch *');
};
Object.defineProperty (Pet.prototype, 'name', {
get: function () {
return this._name;
}
});
var Cat = function (name) {
Pet.call (this, name);
};
require ( 'util') inherits (Cat, Pet).;
Cat.prototype.sayHello = function () {
Pet.prototype.sayHello.call (this);
console.log ( 'miaaaauw');
};
Fortunately, we can now use the Node ES6 syntax:
class Pet {
constructor (name) {
this._name = name;
}
sayHello () {
console.log ( '* scratch *');
}
get name () {
return this._name;
}
}
class Cat extends Pet {
constructor (name) {
super (name);
}
sayHello () {
super.sayHello ();
console.log ( 'miaaaauw');
}
}
Keyword expansion, constructors, call the super class and attributes, is not it great? For more, check out MDN comprehensive guide.
3. Arrow Functions (arrow function)
Often function dynamically bound this will lead to some confusion, people typically use the following ways:
Cat.prototype.notifyListeners = function () {
var self = this;
this._listeners.forEach (function (listener) {
self.notifyListener (listener);
});
};
// OR
Cat.prototype.notifyListeners = function () {
this._listeners.forEach (function (listener) {
this.notifyListener (listener);
} .bind (This));
};
And now, you can use the arrow function:
Cat.prototype.notifyListeners = function () {
this._listeners.forEach ((listener) => {
this.notifyListener (listener);
});
};
View more detailed information about the function of the arrow
4. Object Literals
By object literals, you can use the following shortcut:
var age = 10, name = 'Petsy', size = 32;
// Instead of this ...
var cat = {
age: age,
name: name,
size: size
};
// ... Do this ...
var cat = {
age,
name,
size
};
In addition, you can also easily add their own function object literals.
5. Promises
No longer need to rely on third-party libraries such as the bluebird or Q, you can use a local Promises. There follows open API:
var p1 = new Promise (function (resolve, reject) {});
var p2 = Promise.resolve (20);
var p3 = Promise.reject (new Error ());
var p4 = Promise.all (p1, p2);
var p5 = Promise.race (p1, p2);
// And obviously
. P1.then (() => {}) catch (() => {});
6. String Methods
There are also some new string method:
// Replace `indexOf ()` in a number of cases
name.startsWith ( 'a')
name.endsWith ( 'c');
name.includes ( 'b');
// Repeat the string three times
name.repeat (3);
Go and tell those kids with Ruby! In addition, treatment of Unicode strings are even better.
7. let and const
Following speculation that the return value of the function call:
var x = 20;
(Function () {
if (x === 20) {
var x = 30;
}
return x;
} ()); // -> Undefined
. Yep, undefined Replace var with let and you get the expected behaviour:
let x = 20;
(Function () {
if (x === 20) {
let x = 30;
}
return x;
} ()); // -> 20
Reason: var is a function scope, but let the block scope (as most people expect that). So we can say, let the var variants. You can get more details in the MDN.
Eggs: Node now also supports the const keyword, and it can prevent you give different values for the same reference.
var MY_CONST = 42; // no, no
const MY_CONST = 42; // yes, yes
MY_CONST = 10 // with const, this is no longer possible
to sum up
Node V4 brings ES6 features far more than that, but I hope these seven examples have been able to convince you to update and use the new version.
There are many other features (such as maps / sets ,, symbols and generators, etc.). Make sure you've seen the Node overview of the ES6. Happy upgrading it ~ |
|
|
|