js常用小技巧黒科技-javascript(js)commonly used skill
3289 ワード
1.234 | 0
~~1.234
1.234 >> 0
+'123'
+new Date()
""+[1, 2 , 3, 3, [2, 3, 4]]
var a=1;
var b=2;
[a, b] = [b, a];
console.log(a, b);
Math.max(...[1,2,3])
Math.min(...[1,2,3])
new Date(...[2018,6,4])
method 1:
[...'string']
method 2:
Array.from('string')
let obj1 = {a:1, b:2};
let obj2 = {b:3, c:4};
{...obj1, ...obj2}
Object.assign(obj1, obj2)
var url = new URL('http://localhost:8000/news?a=1&b=2&c=3');
var searchParams = url.searchParams;
for (let key of searchParams.keys()){
console.log('====');
console.log('key === ', key);
console.log('value === ', searchParams.get(key))
}
function preFixNum(num, length) {
return (Array(length).join('0') + num).slice(-length);
}
[...new Set(arr.map(v => JSON.stringify(v)))].map(v => JSON.parse(v))
var arr = [1,2,3,4,5];
method 1:
var sum = eval(arr.join('+'));
method 2:
var sum = arr.reduce((prev,cur) => prev + cur);
let money = 11111;
method 1:
money.toLocaleString('en-US');
method 2:
Intl.NumberFormat().format(money);
method 3:
String(money).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
isTrue && console.log(1);
function RGBtoHEX(rgb){
return ((1<<24) + (rgb.r<<16) + (rgb.g<<8) + rgb.b).toString(16).substr(1);
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
Array.from(new Array(10).keys());
let a = ' '.link('https://github.com/TigerHee/shareJS');
console.log('a === ', a)
'tigerHee'
/(tigerHee)/
'tigerHee'
/(?:tigerHee)/
'tiger' 'tiger' 'Hee'
/tiger(?=Hee)/
'Hee' 'Hee' 'tiger'
/(?<=tiger)Hee/
'tiger' 'tiger' 'java'
/tiger(?!java)/
ギthubをクリックして、もっと多くの知識を得てください。励ましてくれてありがとうございます.