~~a
或
a|0
a+.5|0
!0 // true!!0 // false
''.concat(a, b, c)
```javascript
let time = +new Date();
if (name === 'XX') {flag = true;
} else {flag = false;
}// 等价于
flag = name === 'XX';
if (name === 'XX') {age = 20;
} else {age = 18;
}// 等价于
age = name === 'XX' ? 20 : 18;
if (arr.length !== 0) {}// 等价于
if (arr.length) {}
if (arr.length === 0) {}// 等价于
if (!arr.length) {}
if (a === 1 || a === 2 || a === 3 || a === 4) {}// 等价于
if ([1, 2, 3, 4].includes(a)) {}
let name = person.name || 'XX'
let name = person && person.name;// 等价于
let name = person?.name;
function setData({ a, b = 2 }) {this.a = a;this.b = b;
}
let obj = {a: 1,b: 2
};let keys = Object.keys(obj); // ['a', 'b']
let obj = {a: 1,b: 2
};let keys = Object.values(obj); // [1, 2]
arr.forEach((item, index) => {item.name = 'XX';
});
let newArr = arr.filter((item, index) => index > 2);
let newArr = arr.map(item => {return Object.assign({}, item, { name: 'XX' });
})
let flag = arr.some((item, index) => item.name === 'XX');
let flag = arr.every((item, index) => !!item.name);
let sum = [1, 2, 3, 4, 5].reduce((acc, current) => {return acc + current;
}, 0);
// before method
if (color === 'red') {switchRedColor();
} else if (color === 'grey') {switchGreyColor();
} else if (color === 'yello') {switchYellowColor();
} else if(color === 'white') {switchWhiteColor();
}// after method1
switch(color) {case 'red':switchRedColor();break;case 'grey':switchGreyColor();break;case 'yello':switchYellowColor();break;case 'white':switchWhiteColor();break;
}// after method2
const colors = {'red': switchRedColor,'grey': switchGreyColor,'yellow': switchYellowColor,'white': switchWhiteColor
};for (color in colors) {colors[color]();
}// after method3
const colors = new Map([['red', switchRedColor],['grey', switchGreyColor],['yellow', switchYellowColor],['white', switchWhiteColor]
]);for (color in colors) {colors[color]();
}
let obj = {name: 'kingX'
};if (obj[name]) {}obj.hasOwnProperty('name'); // true
'name' in obj // trueobj.hasOwnProperty('valueOf') // false
'valueOf' in obj // true
arr.indexOf(text) > -1// 等价于
arr.includes(text)// 等价于
~~!arr.indexOf(text)
function getIndex(ev) {let ev = ev || event;let target = ev.target;return [].indexOf.call(document.querySelectorAll('li'). target); // 1
}
上一篇:算命中六合和三合是什么意思
下一篇:说字结尾的成语