ES6, 全称 ECMAScript 6.0 ,是 JavaScript 的下一个版本标准,2015.06 发版。
声明一个只读的常量,一旦声明,常量的值就不能改变。
//基本
const [a, b, c] = [1, 2, 3];
// a = 1
// b = 2
// c = 3
//剩余运算符
const [a, ...b] = [1, 2, 3];
//a = 1
//b = [2, 3]
//字符串
const [a, b, c, d, e] = "hello";
// a = 'h'
// b = 'e'
// c = 'l'
// d = 'l'
// e = 'o'
/* 基本 */
const { name, age } = { name: "张三", age: 18 };
// name = '张三'
// age = 18
/* 别名 */
const { name: nickName } = { name: "李四" };
// nickName = '李四'
/* 嵌套的对象 */
const people = {
name: "张三",
age: 18,
address: {
city: "北京",
street: "朝阳区"
}
};
const {
address: { city, street }
} = people;
/* 剩余运算符 */
const { a, b, ...rest } = { a: 10, b: 20, c: 30, d: 40 };
// a = 10
// b = 20
// rest = {c: 30, d: 40}
ES6 引入了一种新的原始数据类型 Symbol ,表示独一无二的值,最大的用法是用来定义对象的唯一属性名。
ES6 数据类型除了 Number 、 String 、 Boolean 、 Object、 null 和 undefined ,还新增了 Symbol 。
Symbol 函数栈不能用 new 命令,因为 Symbol 是原始数据类型,不是对象。可以接受一个字符串作为参数,为新创建的 Symbol 提供描述,用来显示在控制台或者作为字符串的时候使用,便于区分。
let sy = Symbol("KK");
console.log(sy); // Symbol(KK)
typeof sy; // "symbol"
// 相同参数 Symbol() 返回的值不相等
let sy1 = Symbol("kk");
sy === sy1; // false
作为属性名
由于每一个 Symbol 的值都是不相等的,所以 Symbol 作为对象的属性名,可以保证属性不重名。
let sy = Symbol("key1");
// 写法1
let syObject = {};
syObject[sy] = "xxxx";
console.log(syObject); // {Symbol(key1): "xxxx"}
// 写法2
let syObject = {
[sy]: "xxxx"
};
console.log(syObject); // {Symbol(key1): "xxxx"}
// 写法3
let syObject = {};
Object.defineProperty(syObject, sy, { value: "xxxx" });
console.log(syObject); // {Symbol(key1): "xxxx"}
let syObject = {};
syObject[sy] = "xxxx";
syObject[sy]; // "xxxx"
syObject.sy; // undefined
const person = new Map();
person.set("name", "张三");
//Map(1) {'name' => '张三'}
person.set("age", 18);
//Map(2) {'name' => '张三', 'age' => 18}
person.get("name");
//'张三'
person.size;
//2
person.has("name");
//true
person.delete("age");
//true
console.log(person);
//Map(1) {'name' => '张三'}
let myMap = new Map();
myMap.set(0, "zero");
myMap.set(1, "one");
myMap.forEach((value, key) => console.log(key + " = " + value));
const numberSet = new Set();
numberSet.add(1);
//Set(1) {1}
numberSet.add(2);
//Set(2) {1, 2}
numberSet.add(1);
//Set(2) {1, 2}
numberSet.delete(1);
//true
numberSet.has(1);
//false
numberSet.size;
//1
numberSet.forEach(number => console.log(number));
// 2
const mySet = new Set([1, 2, 3, 4, 4]);
[...mySet]; // [1, 2, 3, 4]
const a = new Set([1, 2, 3]);
const b = new Set([4, 3, 2]);
const union = new Set([...a, ...b]); // {1, 2, 3, 4}
const a = new Set([1, 2, 3]);
const b = new Set([4, 3, 2]);
const intersect = new Set([...a].filter(x => b.has(x))); // {2, 3}
const a = new Set([1, 2, 3]);
const b = new Set([4, 3, 2]);
const difference = new Set([...a].filter(x => !b.has(x))); // {1}
console.log("Hello,".repeat(2)); // "Hello,Hello,"
console.log("h".padStart(5, "o")); // "ooooh"
console.log("h".padEnd(5, "o")); // "hoooo"
console.log("h".padStart(5)); // " h"
const name = "Mike";
const age = 27;
const info = `My Name is ${name},I am ${age + 1} years old next year.`;
// 报错
var f = (id,name) => {id: id, name: name};
f(6,2); // SyntaxError: Unexpected token :
// 不报错
var f = (id,name) => ({id: id, name: name});
f(6,2); // {id: 6, name: 2}
function fn(name, age = 17) {
console.log(name + "," + age);
}
fn("Amy", 18); // Amy,18
fn("Amy", ""); // Amy,
fn("Amy"); // Amy,17
//只有在未传递参数,或者参数为 undefined 时,才会使用默认参数,null 值被认为是有效的值传递。
function fn1(name, age = 17) {
console.log(name + "," + age);
}
fn1("Amy", null); // Amy,null
function f(...values) {
console.log(values.length);
}
f(1, 2); //2
f(1, 2, 3, 4); //4
应用场景是异步请求或是异步函数需要等待执行结果
<script>
methods:{
getData1() {
return axios.get('http://111111111111')
}
getData2() {
return axios.get('http://222222222222')
}
getData3() {
return axios.get('http://333333333333')
}
}
mounted(){
//依次调用上面三个方法,then的参数res是上一个peomise返回的结果
this.getData1().then(res1 => {
console.log(res1)
return this.getData2()
}).then(res2 => {
console.log(res2)
return this.getData3()
}).then(res3 => {
console.log(res3)
}).catch(err => {
console.log(err)
})
}
</script>


async 是 ES7 才有的与异步操作有关的关键字,和 Promise , Generator 有很大关联的。
async function helloAsync() {
return "helloAsync";
}
console.log(helloAsync()); // Promise {<resolved>: "helloAsync"}
helloAsync().then(v => {
console.log(v); // helloAsync
});
function testAwait() {
return new Promise(resolve => {
setTimeout(function () {
console.log("testAwait");
resolve();
}, 1000);
});
}
async function helloAsync() {
await testAwait();
console.log("helloAsync");
}
helloAsync();
// testAwait
// helloAsync
await 针对所跟不同表达式的处理方式:
//一次性导入所有export导出的方法
import * as xxxx from “./xxxx”
// 按需引入export导出的方法
import { xx } from “./xxxx”
// 引入export default的方法
import xx from “./xxxx”
exports.xxx = xxxx;
module.exports = {}
module.exports = getList()=>{};
const xxxx = require('模块')
src = require("./src/img/xxx.png");
@import url("~@/css/xxx.css");