更加优先使用let 和 const去取代varjson
/* let a=1; let b={x:'test'}; let c=[1,2,3]; */ let [a, b, c] = [1, {x: 'test'}, [1, 2, 3]]; console.log(a); //1 console.log(b.x); //test console.log(c.length); //3
let [x = '1'] = []; console.log(x); //1 let [y, z = '2'] = ['1']; console.log(y); //1 console.log(z); //2
let [x, y] = [1, 2]; [x, y] = [y, x]; console.log(`x=${x} y=${y}`); //x=2 y=1
let jsonData = { id: 42, status: "OK", data: [867, 5309] }; let { id, status, data } = jsonData; console.log(id, status, data); // 42, "OK", [867, 5309]
const { SourceMapConsumer, SourceNode } = require("source-map");
let str='hello'; for(let i of str){ console.log(i); } /* h e l l o */
let str='hello'; console.log(str.startsWith('he')); //true console.log(str.endsWith('lo')); //true console.log(str.includes('ll')); //true
let a="test"; console.log(`I am ${a}`); //I am test
function add(...values) { let sum = 0; for (var val of values) { sum += val; } return sum; } console.log(add(2, 5, 3)) // 10
/* function test(x,y){ return x+y; } */ const test=(x,y)=>x+y;
使用箭头函数能够避免ES5函数this的问题,箭头函数this在方法内和方法外是同样的数组
扩展运算符(spread)是三个点(...)。它比如 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。函数
console.log(...[1, 2, 3]) // 1 2 3 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5
解决==类型转换和===的NaN不等于自身,以及+0等于-0。ui
对象的浅拷贝this
JSON.parse(JSON.stringify(data));