JS简写技巧提升工作效率

 

let x, y = 20; // 声明同值变量

let [a, b, c] = [5, 8, 12]; // 利用解构,可为多个变量同时赋值

[x, y] = [y, x];

[1, 'one', 2, 'two'].indexOf(value) >= 0

[1, 'one', 2, 'two'].includes(value)

arr2 = arr1.concat([60, 80])
arr2 = [...arr1, 60, 80]

const power = 4**3; // 64     替换Math.pow()

const floor = ~~6.8; // 6     Math.floor()
let result = marks >= 30 ? 'Pass' : 'Fail';

let imagePath = getImagePath() || 'default.jpg';

isLoggedin && goToHomepage();

 删除对象属性的写法

let obj = {x: 45, y: 72, z: 68, p: 98};
 
let {x, p, ...newObj} = obj;
console.log(newObj); // {y: 72, z: 68}

使用arr.filter(Boolean)过滤掉数组成员的值false

let arr = [12, null, 0, 'xyz', null, -25, NaN, '', undefined, 0.5, false];

let filterArray = arr.filter(Boolean)

 

posted @ 2022-01-20 21:48  Dz&Ying  阅读(32)  评论(0)    收藏  举报