1.let和const
let
:用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效,即let声明的是一个块作用域内的变量。
const
:声明一个只读的常量。一旦声明,常量的值就不能改变。const实际上保证的是变量指向的那个内存地址所保存的数据不得更改。对于简单的数据(数值、字符串、布尔值)值就保存在变量指向的那个内存地址,等同于常量,但对于复合类型的数据(主要是对象和数组)变量指向的内存地址,保存的只是一个指向实际数据的指针,const只能保证这个指针是固定的,至于它指向的数据结构是不是可变的,就完全不能控制了。因此,将一个对象声明为常量必须非常小心。
特点:
- 不存在变量提升
- 暂时性死区----只要块级作用域内存在let命令,它所声明的变量就“绑定”这个区域,不再受外部的影响。
- 不允许重复声明
- 块级作用域----被{}包裹的,外部不能访问内部
- 当使用常量const声明时,请使用大写变量,如:CAPITAL_CASING
- const在声明时必须被赋值
2.模板字符串
ES5时,我们往往这么处理模板字符串:通过“\”和“+”来构建模板
$("body").html("This demonstrates ”+ name + ", " + seatNumber + ", " + sex + " and so on.");
ES6:将表达式嵌入字符串中进行拼接,用${}+``反引号
$("body").html(`This demonstrates ${name}, ${seatNumber}, ${sex} and so on.`);
3.箭头函数
ES6中,箭头函数就是函数的一种简写形式,使用括号包裹参数,跟随一个=>,紧接着是函数体
箭头函数特点:
- 不需要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;
4.函数的参数默认值
// 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
5.Spread(拓展)/Rest(剩余)操作符
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 操作符
function foo(...args) {
console.log(args);
}
foo( 1, 2, 3, 4, 5); // [1, 2, 3, 4, 5]
6.二进制和八进制字面量
ES6支持二进制和八进制的字面量,通过在数字前面添加0o或者0O即可将其转换为八进制
let oValue = 0o10;
console.log(oValue); // 8
let bValue = 0b10; // 二进制使用 `0b` 或者 `0B`
console.log(bValue); // 2
7.对象和数组解构
// 对象
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);
var foo = ["one", "two", "three", "four"];
var [one, two, three] = foo;
console.log(one); // "one"
console.log(two); // "two"
console.log(three); // "three"
//如果你要忽略某些值,你可以按照下面的写法获取你想要的值
var [first, , , last] = foo;
console.log(first); // "one"
console.log(last); // "four"
//你也可以这样写
var a, b; //先声明变量
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
8.对象超类
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
9.for … of和for … in
for…of用于遍历一个迭代器,如数组:
for … in:用来遍历对象中的属性
10.ES6中的类
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()。
有几点值得注意的是:
1.类的声明不会提升,如果你要使用某个Class,那你必须在使用之前定义它,否则会抛出一个 ReferenceError 的错误
2.在类中定义函数不需要使用function关键字