console.log(a);
var a=1;
// undefined
console.log(b);
let b=1;
// 会报错
// 这是应为var声明会提升,实际上var a是在console前的,所以会undefined,而let会直接报错
let {a, b} = {a:1, b:2}; // a => 1 , b => 2
// 交换赋值
let [a,b] = [b,a];
// 提取json数据
let json = {
name : ‘leo‘,
age: 18
}
let {name, age} = json;
console.log(name,age); // leo, 18
// 遍历Map结构
const m = new Map();
m.set(‘a‘,1);
m.set(‘b‘,2);
for (let [k, v] of m){
console.log(k + ‘ : ‘ + v);
}
// 用于按需加载模块中需要用到的方法。
const {log, sin, cos} = require(‘math‘);
let a = `abc${v1}def`
function f(a, b=‘leo‘){
console.log(a, b);
}
f(‘hi‘); // hi leo
f(‘hi‘, ‘jack‘); // hi jack
f(‘hi‘, ‘‘); // hi leo
()=>{}
let a = [1, 2, 3];
let b = [4, 5, 6];
console.log([...a, ...b]);//[1,2,3,4,5,6]
let a = ‘a1‘;
let b = { a }; // b => { a : ‘a1‘ }
// 等同于
let b = { a : a };
Object.is(‘a‘,‘a‘); // true
Object.is({}, {}); // false
// 没有参数
let a1 = Symbol();
let a2 = Symbol();
a1 === a2; // false
// 有参数
let a1 = Symbol(‘abc‘);
let a2 = Symbol(‘abc‘);
a1 === a2; // false
// add方法来添加新成员。
let a = new Set();
[1,2,2,1,3,4,5,4,5].forEach(x=>a.add(x));
for(let k of a){
console.log(k)
};
// 基础使用
let a = new Set([1,2,3,3,4]);
[...a]; // [1,2,3,4]
a.size; // 4
// 数组去重
[...new Set([1,2,3,4,4,4])];// [1,2,3,4]
//增删改查
let a = new Set();
a.add(1).add(2); // a => Set(2)?{1, 2}
a.has(2); // true
a.has(3); // false
a.delete(2); // true a => Set(1)?{1}
a.clear(); // a => Set(0)?{}
// 数据类型不会转换
[...new Set([5,‘5‘])]; // [5, "5"]
// 遍历和过滤
let a = new Set([1,2,3,4]);
// map 遍历操作
let b = new Set([...a].map(x =>x*2));// b => Set(4)?{2,4,6,8}
// filter 过滤操作
let c = new Set([...a].filter(x =>(x%2) == 0)); // b => Set(2) {2,4}
// 获取交集 并集 差集
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
// 并集
let c1 = new Set([...a, ...b]); // Set {1,2,3,4}
// 交集
let c2 = new Set([...a].filter(x => b.has(x))); // set {2,3}
// 差集
let c3 = new Set([...a].filter(x => !b.has(x))); // set {1}
let a = new Map();
let b = {name: ‘leo‘ };
a.set(b,‘my name‘); // 添加值
a.get(b); // 获取值
a.size; // 获取总数
a.has(b); // 查询是否存在
a.delete(b); // 删除一个值
a.clear(); // 清空所有成员 无返回
let a = new Map([
[‘name‘,‘leo‘],
[‘age‘,18]
])
let a1 = [...a.keys()]; // a1 => ["name", "age"]
let a2 = [...a.values()]; // a2 => ?["leo", 18]
let a3 = [...a.entries()];// a3 => [[‘name‘,‘leo‘], [‘age‘,18]]
let a = [ [‘name‘,‘leo‘], [1, ‘hi‘ ]]
let b = new Map(a);
function fun(s) {
let obj = Object.create(null);
for (let [k,v] of s) {
obj[k] = v;
}
return obj;
}
const a = new Map().set(‘yes‘, true).set(‘no‘, false);
fun(a)
function fun(obj) {
let a = new Map();
for (let k of Object.keys(obj)) {
a.set(k, obj[k]);
}
return a;
}
fun({yes: true, no: false})
// Map键名都是字符串,转为对象JSON:
function fun (s) {
let obj = Object.create(null);
for (let [k,v] of s) {
obj[k] = v;
}
return JSON.stringify(obj)
}
let a = new Map().set(‘yes‘, true).set(‘no‘, false);
fun(a);// ‘{"yes":true,"no":false}‘
// Map键名有非字符串,转为数组JSON:
function fun (map) {
return JSON.stringify([...map]);
}
let a = new Map().set(true, 7).set({foo: 3}, [‘abc‘]);
fun(a)
// ‘[[true,7],[{"foo":3},["abc"]]]‘
// 所有键名都是字符串:
function fun (s) {
let strMap = new Map();
for (let k of Object.keys(s)) {
strMap.set(k, s[k]);
}
return strMap;
return JSON.parse(strMap);
}
fun(‘{"yes": true, "no": false}‘);// Map {‘yes‘ => true, ‘no‘ => false}
// 整个 JSON 就是一个数组,且每个数组成员本身,又是一个有两个成员的数组:
function fun2(s) {
return new Map(JSON.parse(s));
}
fun2(‘[[true,7],[{"foo":3},["abc"]]]‘)
// Map {true => 7, Object {foo: 3} => [‘abc‘]}
let p = new Promise(function (resolve, reject){
if(/*异步操作成功*/){
resolve(value);
} else {
reject(error);
}
})
// resolve作用是将Promise的状态从pending变成resolved,在异步操作成功时调用,返回异步操作的结果,作为参数传递出去。
// reject作用是将Promise的状态从pending变成rejected,在异步操作失败时报错,作为参数传递出去。
// demo
const p1 = new Promise(function(resolve, reject) {
setTimeout(() => resolve(console.log(123)), 3000)
})
const p2 = new Promise(function(resolve, reject) {
setTimeout(() => resolve(p1), 1000)
})
p2
.then(result => console.log(result))
.catch(error => console.log(error))
// 123
// promise接受两个参数resolve,reject,内部函数成功则触发resolve,失败则触发reject
const p = new Promise(function(resolve, reject) {
resolve(‘ok‘);
throw new Error(‘test‘);
});
p
.then(function(value) { console.log(value) }) // ok
.catch(function(error) { console.log(error) }); // 捕获错误
.finally(() => {···}) // 失败成功都会执行
const p = Promise.all([p1, p2, p3]);
const p = Promise.race([p1, p2, p3]);
原文:https://www.cnblogs.com/facy/p/13600982.html