Home > Web Front-end > JS Tutorial > body text

Overview of ES6's new features_javascript skills

WBOY
Release: 2016-05-16 15:11:22
Original
1299 people have browsed it

Nick Justice is a member of the GitHub Developer Program. Long before the ES6 language standard was released, he used transpilers like Babel and the latest versions of browsers to use ES6 features in his projects. He believes that the new features of ES6 will greatly change the way JavaScript is written.

ES6 (ECMAScript 6) is the standard for the upcoming new version of the JavaScript language, codenamed harmony (meaning harmony, obviously it has not kept up with the pace of our country, we have entered the Chinese Dream version). The last time a standard was formulated was ES5, which was released in 2009. The standardization work of ES6 is currently in progress, and the officially finalized version is expected to be released in December 2014. But most standards are already in place, and browser support for ES6 is being implemented.

Although technology is developing too fast, if we don’t stop learning, we will not be eliminated by new technologies. Let’s learn the new features of es6 together.

Arrow Operator

If you know C# or Java, you must know lambda expressions. The new arrow operator => in ES6 has the same purpose. It simplifies writing functions. The left side of the operator is the input parameters, and the right side is the operation performed and the returned value Inputs=>outputs.

We know that callbacks are common in JS, and generally callbacks appear in the form of anonymous functions. It is necessary to write a function every time, which is very cumbersome. When the arrow operator is introduced, callbacks can be easily written. Please see the example below:

var array = [1,2,3];
// 传统写法写法
array.forEach(function(v) {
console.log(v);
});
// ES6写法
array.forEach(v => console.log(v)); 
Copy after login

class support

ES6 added support for classes and introduced the class keyword (in fact, class has always been a reserved word in JavaScript, the purpose is to consider that it may be used in new versions in the future, and now it finally comes in handy) . JS itself is object-oriented, and the classes provided in ES6 are actually just wrappers of the JS prototype pattern. Now that native class support is provided, object creation and inheritance are more intuitive, and concepts such as parent class method invocation, instantiation, static methods, and constructors are more visual.

The following code shows the use of classes in ES6:

// 类的定义
class Animal {
// ES6中新型构造器
constructor(name) {
this.name = name;
}
// 实例方法
sayName() {
console.log('My name is ' + this.name);
}
}
// 类的继承
class Programmer extends Animal {
constructor(name) {
// 直接调用父类构造器进行初始化
super(name);
}
program() {
console.log("I'm coding...");
}
}
// 测试我们的类
var animal = new Animal('dummy'),
wayou = new Programmer('wayou');
animal.sayName(); // 输出 'My name is dummy'
wayou.sayName(); // 输出 'My name is wayou'
wayou.program(); // 输出 'I'm coding...' 
Copy after login

Enhanced object literals

Object literals have been enhanced, the writing method is more concise and flexible, and more things can be done when defining objects. Specifically shown in:

1. Prototypes can be defined in object literals

2. You can define methods without the function keyword

3. Directly call the parent class method

In this way, object literals are more consistent with the class concept mentioned above, making it easier and more convenient to write object-oriented JavaScript.

// 通过对象字面量创建对象
var human = { 
breathe() {
console.log('breathing...');
}
};
var worker = { 
__proto__: human, // 设置此对象的原型为human, 相当于继承human
company: 'freelancer',
work() {
console.log('working...');
}
};
human.breathe(); // 输出 'breathing...'
// 调用继承来的breathe方法
worker.breathe(); // 输出 'breathing...' 
Copy after login

String template

String templates are relatively simple and easy to understand. ES6 allows the use of backticks ` to create strings. The string created by this method can contain variables ${vraible} wrapped by a dollar sign and curly brackets. If you have used a back-end strongly typed language such as C#, you should be familiar with this feature.

// 产生一个随机数
var num = Math.random();
// 将这个数字输出到console 
console.log(`your num is ${num}`);
Copy after login

Deconstruction

Automatically parse values ​​in arrays or objects. For example, if a function wants to return multiple values, the conventional approach is to return an object and return each value as a property of the object. But in ES6, using the feature of destructuring, you can directly return an array, and then the values ​​in the array will be automatically parsed into the corresponding variable that receives the value.

function getVal() {
return[1,2];
}
var [x,y] = getVal(), // 函数返回值的解构
console.log('x:' + x + ', y:' + y); // 输出:x:1, y:2
[name,,age] = ['wayou','male','secrect']; // 数组解构
console.log('name:' + name + ', age:' + age); //输出:name:wayou, age:secrect 
Copy after login

Parameter default values, variable parameters, extended parameters

Default parameter value

You can now specify the default values ​​of parameters when defining a function, instead of using the logical OR operator as before.

function sayHello(name) {
// 传统的指定默认参数的方式
var name = name || 'dude';
console.log('Hello ' + name);
} 
sayHello(); // 输出:Hello dude
sayHello('Wayou'); // 输出:Hello Wayou
// 运用ES6的默认参数
function sayHello2(name = 'dude') {
console.log(`Hello${name}`);
}
sayHello2(); // 输出:Hello dude
sayHello2('Wayou'); // 输出:Hello Wayou 
Copy after login

Indefinite parameters

Indefinite parameters are the use of named parameters in a function while receiving an indefinite number of unnamed parameters. This is just syntactic sugar, and in previous JavaScript code we could achieve this through the arguments variable. The format of the variable parameters is three periods followed by the variable names representing all variable parameters.

For example, in the following example,...x represents all parameters passed into the add function.

// 将所有参数相加的函数
function add(...x) {
return x.reduce((m,n) => m+n);
}
// 传递任意个数的参数
console.log(add(1,2,3)); // 输出:6
console.log(add(1,2,3,4,5)); // 输出:15 
Copy after login

Extended parameters

Extended parameters are another form of syntax sugar, which allows passing arrays or array-like parameters directly as function parameters without going through apply.

var people = ['Wayou','John','Sherlock'];
// sayHello函数本来接收三个单独的参数人一,人二和人三
function sayHello(people1, people2, people3) {
console.log(`Hello${people1}, ${people2}, ${people3}`);
}
// 但是我们将一个数组以拓展参数的形式传递,它能很好地映射到每个单独的参数
sayHello(...people); // 输出:Hello Wayou,John,Sherlock
// 而在以前,如果需要传递数组当参数,我们需要使用函数的apply方法
sayHello.apply(null, people); // 输出:Hello Wayou,John,Sherlock 
Copy after login

let and const keywords

You can think of let as var, except that the variables it defines can only be used within a specific scope, and are invalid outside this scope. const is very intuitive and is used to define constants, that is, variables whose values ​​cannot be changed.

for (let i=0; i<2; i++) {
console.log(i); // 输出: 0,1
}
console.log(i); // 输出:undefined,严格模式下会报错 
Copy after login

for of 值遍历

我们都知道for in循环用于遍历数组,类数组或对象,ES6中新引入的for of循环功能相似,不同的是每次循环它提供的不是序号而是值。

var someArray = ["a","b","c"];
for (v of someArray) {
console.log(v); // 输出 a,b,c
} 
Copy after login

iterator, generator

1.iterator: 它是这么一个对象,拥有一个next方法,这个方法返回一个对象{done,value},这个对象包含两个属性,一个布尔类型的done和包含任意值的value

2.iterable: 这是这么一个对象,拥有一个obj[@@iterator]方法,这个方法返回一个iterator

3.generator: 它是一种特殊的iterator。反的next方法可以接收一个参数并且返回值取决与它的构造函数(generator function)。generator同时拥有一个throw方法

4.generator函数: 即generator的构造函数。此函数内可以使用yield关键字。在yield出现的地方可以通过generator的next或throw方法向外界传递值。generator 函数是通过function*来声明的

5.yield关键字:它可以暂停函数的执行,随后可以再进进入函数继续执行

模块

在ES6标准中,JavaScript原生支持module了。这种将JS代码分割成不同功能的小块进行模块化的概念是在一些三方规范中流行起来的,比如CommonJS和AMD模式。

将不同功能的代码分别写在不同文件中,各模块只需导出公共接口部分,然后通过模块的导入的方式可以在其他地方使用。

// point.js
module "point" {
export class Point {
constructor(x,y) {
public x=x;
public y=y;
}
}
}
// myapp.js
// 声明引用的模块 
module point from "/point.js";
// 这里可以看出,尽管声明了引用的模块,还是可以通过指定需要的部分进行导入
import Point from "point";
var origin = new Point(0,0);
console.log(origin); 
Map,Set 和 WeakMap,WeakSet
Copy after login

这些是新加的集合类型,提供了更加方便的获取属性值的方法,不用像以前一样用hasOwnProperty来检查某个属性是属于原型链上的呢还是当前对象的。同时,在进行属性值添加与获取时有专门的get,set方法。

// Sets 
var s = new Set();
s.add("hello").add("goodbye");
s.size === 2;
s.has("hello") === true;
// Maps
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34; 
有时候我们会把对象作为一个对象的键用来存放属性值,普通集合类型比如简单对象会阻止垃圾回收器对这些作为属性键存在的对象的回收,有造成内存泄漏的危险。而WeakMap,WeakSet则更加安全些,这些作为属性键的对象如果没有别的变量在引用它们,则会被回收释放掉,具体还看下面的例子:
// Weak Maps
var wm = new WeakMap();
wm.set(s, {extra: 42});
wm.size === undefined // 对象释放了
// Weak Sets
var ws = new WeakSet();
ws.add({data: 42}); // 因为添加到ws的这个临时对象没有其他变量引用它,所以ws不会保存它的值,也就是说这次添加其实没有意思 
Proxies
Proxy可以监听对象身上发生了什么事情,并在这些事情发生后执行一些相应的操作。一下子让我们对一个对象有了很强的追踪能力,同时在数据绑定方面也很有用处。
// 定义被侦听的目标对象
var engineer = {name: 'Joe Sixpack', salary: 50};
// 定义处理程序
var interceptor = {set: function(receiver, property, value) {
console.log(property, 'is changed to', value);
receiver[property] = value;
}
};
// 创建代理以进行侦听
engineer = Proxy(engineer, interceptor);
//做一些改动来触发代理
engineer.salary = 60; // 控制台输出:salary is changed to 60 
Copy after login

上面代码我已加了注释,这里进一步解释。对于处理程序,是在被侦听的对象身上发生了相应事件之后,处理程序里面的方法就会被调用,上面例子中我们设置了set的处理函数,表明,如果我们侦听的对象的属性被更改,也就是被set了,那这个处理程序就会被调用,同时通过参数能够得知是哪个属性被更改,更改为了什么值。

Symbols

我们知道对象其实是键值对的集合,而键通常来说是字符串。而现在除了字符串外,我们还可以用symbol这种值来做为对象的键。Symbol是一种 基本类型,像数字,字符串还有布尔一样,它不是一个对象。Symbol 通过调用symbol函数产生,它接收一个可选的名字参数,该函数返回的symbol是唯一的。之后就可以用这个返回值做为对象的键了。Symbol还可以用来创建私有属性,外部无法直接访问由symbol做为键的属性值。

(function() {
// 创建 symbol
var key = Symbol("key");
function MyClass(privateData) {
this[key] = privateData;
}
MyClass.prototype = {
doStuff: function() {
...this[key]...
}
};
})();
var c = new MyClass("hello");
c["key"] === undefined //无法访问该属性,因为是私有的 
Math,Number,String,Object 的新API
对Math,Number,String还有Object等添加了许多新的API。
// Number
// 不同的两个Number之间的最小的差
Number.EPSILON
// 判断是否是整数
Number.isInteger(Infinity) // false
// 判断是否为非数字
Number.isNaN("NaN") // false
// Math
Math.acosh(3) // 1.762747174039086
Math.hypot(3,4) // 5
Math.imul(Math.pow(2,32)-1, Math.pow(2,32)-2) // 2
// String
"abcde".contains("cd") // true
"abc".repeat(3) // "abcabcabc"
// Array
// 将一个类数组对象或可迭代对象转换成真实的数组
Array.from(document.querySelectorAll('*'))
// 将它的任意类型的多个参数放在一个数组里并返回
Array.of(1,2,3)
// 将一个数组中指定区间的所有元素的值, 都替换成或者说填充成为某个固定的值
[0,0,0].fill(7,1) // [0,7,7]
// 用来查找数组中某指定元素的索引, 如果找不到指定的元素, 则返回 -1
[1,2,3].findIndex(x => x == 2) // 1
// 返回一个 Array Iterator 对象,该对象包含数组中每一个索引的键值对
["a","b","c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
// 返回一个数组索引的迭代器
["a","b","c"].keys() // iterator 0, 1, 2
// 返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值
["a","b","c"].values() // iterator "a", "b", "c"
// Object
Object.assign(Point, {origin: new Point(0,0)}) 
Promises
Promises是处理异步操作的一种模式,之前在很多三方库中有实现,比如jQuery的deferred 对象。当你发起一个异步请求,并绑定了.when(), .done()等事件处理程序时,其实就是在应用promise模式。
// 创建 promise
var promise = new Promise(function(resolve,reject) {
// 进行一些异步或耗时操作
if(/*如果成功 */) {
resolve("Stuff worked!");
} else {
reject(Error("It broke"));
}
});
// 绑定处理程序
promise.then(function(result) {
// promise成功的话会执行这里
console.log(result); // "Stuff worked!"
}, function(err) {
// promise失败会执行这里
console.log(err); // Error: "It broke"
});
Copy after login

有关ES6的新特性就给大家介绍这么多,希望对大家有所帮助!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!