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 基本类型。BigInt 是后来在 ES2020 中加入的。
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' => '张三'}
key 为字符串 key 为对象 key 为函数 key 为 NAN
一般使用 for...of 和 forEach
let myMap = new Map();
myMap.set(0, "zero");
myMap.set(1, "one");
myMap.forEach((value, key) => console.log(key + " = " + value));
Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。 Set 内的值都是唯一的,没有重复的值。
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}
repeat():返回新的字符串,表示将字符串重复指定次数返回。
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"
变量名写在 ${} 中,${} 中可以放入 JavaScript 表达式。
const name = "Mike";
const age = 27;
const info = `My Name is ${name},I am ${age + 1} years old next year.`;
当参数只有一个时,可以不写( ) 当箭头函数可以一行写完时,可以不写{ },并且可以不 retuen 因为隐试返回了
// 报错
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
是异步编程的一种解决方案。
从语法上说,Promise 是一个对象,从它可以获取异步操作的消息。
Promise 异步操作有三种状态:pending(进行中)、fulfilled(已成功)和 rejected(已失败)。除了异步操作的结果,任何其他操作都无法改变这个状态。
Promise 对象只有:从 pending 变为 fulfilled 和从 pending 变为 rejected 的状态改变。只要处于 fulfilled 和 rejected ,状态就不会再变了,即 resolved(已定型)。
应用场景是异步请求或是异步函数需要等待执行结果
<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/await在 ES2017 中标准化,用于以更直观的方式编写基于 Promise 的异步代码。
async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数。
async function helloAsync() {
return "helloAsync";
}
console.log(helloAsync()); // Promise {<resolved>: "helloAsync"}
helloAsync().then(v => {
console.log(v); // helloAsync
});
async 函数中可能会有 await 表达式,async 函数执行时,如果遇到 await 就会先暂停执行 ,等到触发的异步操作完成后,恢复 async 函数的执行并返回解析值。
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 单独导出 module.exports 默认导出 require 引入模块
exports.xxx = xxxx;
module.exports = {}
module.exports = getList()=>{};
const xxxx = require('模块')
src = require("./src/img/xxx.png");
@import url("~@/css/xxx.css");