ECMAScript 6(ES6) 目前基本成为业界标准,它的普及速度比 ES5 要快不少,主要缘由是现代浏览器对 ES6 的支持至关迅速,尤为是 Chrome 和 Firefox 浏览器,已经支持 ES6 中绝大多数的特性。html
下面逐一为你们详解经常使用的ES6新特性:数组
ES6推荐使用let声明局部变量,相比以前的var(不管声明在何处,都会被视为声明在函数的最顶部) let和var声明的区别:浏览器
var x = '全局变量';
{
let x = '局部变量';
console.log(x); // 局部变量
}
console.log(x); // 全局变量
复制代码
let表示声明变量,而const表示声明常量,二者都为块级做用域;const 声明的变量都会被认为是常量,意思就是它的值被设置完成后就不能再修改了:bash
const a = 1
a = 0 //报错
复制代码
若是const的是一个对象,对象所包含的值是能够被修改的。抽象一点儿说,就是对象所指向的地址没有变就行:函数
const student = { name: 'cc' }
student.name = 'yy';// 不报错
student = { name: 'yy' };// 报错
复制代码
有几个点须要注意:ui
- let 关键词声明的变量不具有变量提高(hoisting)特性
- let 和 const 声明只在最靠近的一个块中(花括号内)有效
- 当使用常量 const 声明时,请使用大写变量,如:CAPITAL_CASING
- const 在声明时必须被赋值
在ES6以前,咱们每每这么处理模板字符串: 经过“\”和“+”来构建模板this
$("body").html("This demonstrates the output of HTML \ content to the page, including student's\ " + name + ", " + seatNumber + ", " + sex + " and so on.");
复制代码
而对ES6来讲spa
$("body").html(`This demonstrates the output of HTML content to the page,
including student's ${name}, ${seatNumber}, ${sex} and so on.`); 复制代码
ES6 中,箭头函数就是函数的一种简写形式,使用括号包裹参数,跟随一个 =>,紧接着是函数体;code
箭头函数最直观的三个特色。cdn
- 不须要 function 关键字来建立函数
- 省略 return 关键字
- 继承当前上下文的 this 关键字
// ES5
var add = function (a, b) {
return a + b;
};
// 使用箭头函数
var add = (a, b) => a + b;
// ES5
[1,2,3].map((function(x){
return x + 1;
}).bind(this));
// 使用箭头函数
[1,2,3].map(x => x + 1);
复制代码
细节:当你的函数有且仅有一个参数的时候,是能够省略掉括号的。当你函数返回有且仅有一个表达式的时候能够省略{} 和 return;
在ES6以前,咱们每每这样定义参数的默认值:
// ES6以前,当未传入参数时,text = 'default';
function printText(text) {
text = text || 'default';
console.log(text);
}
// ES6;
function printText(text = 'default') {
console.log(text);
}
printText('hello'); // hello
printText();// default
复制代码
Spread / Rest 操做符指的是 ...,具体是 Spread 仍是 Rest 须要看上下文语境。
当被用于迭代器中时,它是一个 Spread 操做符:
function foo(x,y,z) {
console.log(x,y,z);
}
let arr = [1,2,3];
foo(...arr); // 1 2 3
复制代码
当被用于函数传参时,是一个 Rest 操做符:当被用于函数传参时,是一个 Rest 操做符:
function foo(...args) {
console.log(args);
}
foo( 1, 2, 3, 4, 5); // [1, 2, 3, 4, 5]
复制代码
ES6 支持二进制和八进制的字面量,经过在数字前面添加 0o 或者0O 便可将其转换为八进制值:
let oValue = 0o10;
console.log(oValue); // 8
let bValue = 0b10; // 二进制使用 `0b` 或者 `0B`
console.log(bValue); // 2
复制代码
// 对象
const student = {
name: 'Sam',
age: 22,
sex: '男'
}
// 数组
// const student = ['Sam', 22, '男'];
// ES5;
const name = student.name;
const age = student.age;
const sex = student.sex;
console.log(name + ' --- ' + age + ' --- ' + sex);
// ES6
const { name, age, sex } = student;
console.log(name + ' --- ' + age + ' --- ' + sex);
复制代码
ES6 容许在对象中使用 super 方法:
var parent = {
foo() {
console.log("Hello from the Parent");
}
}
var child = {
foo() {
super.foo();
console.log("Hello from the Child");
}
}
Object.setPrototypeOf(child, parent);
child.foo(); // Hello from the Parent
// Hello from the Child
复制代码
for...of 用于遍历一个迭代器,如数组:
let letter = ['a', 'b', 'c'];
letter.size = 3;
for (let letter of letters) {
console.log(letter);
}
// 结果: a, b, c
复制代码
for...in 用来遍历对象中的属性:
let stu = ['Sam', '22', '男'];
stu.size = 3;
for (let stu in stus) {
console.log(stu);
}
// 结果: Sam, 22, 男
复制代码
ES6 中支持 class 语法,不过,ES6的class不是新的对象继承模型,它只是原型链的语法糖表现形式。
函数中使用 static 关键词定义构造函数的的方法和属性:
class Student {
constructor() {
console.log("I'm a student.");
}
study() {
console.log('study!');
}
static read() {
console.log("Reading Now.");
}
}
console.log(typeof Student); // function
let stu = new Student(); // "I'm a student."
stu.study(); // "study!"
stu.read(); // "Reading Now."
复制代码
类中的继承和超集:
class Phone {
constructor() {
console.log("I'm a phone.");
}
}
class MI extends Phone {
constructor() {
super();
console.log("I'm a phone designed by xiaomi");
}
}
let mi8 = new MI();
复制代码
extends 容许一个子类继承父类,须要注意的是,子类的constructor 函数中须要执行 super() 函数。 固然,你也能够在子类方法中调用父类的方法,如super.parentMethodName()。 在 这里 阅读更多关于类的介绍。
有几点值得注意的是:
- 类的声明不会提高(hoisting),若是你要使用某个 Class,那你必须在使用以前定义它,不然会抛出一个 ReferenceError 的错误
- 在类中定义函数不须要使用 function 关键词