<script>
function test1(){
// 如何装逼用代码骂别人 SB
console.log('(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]',(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]);
}
function test2(){
// 如何用代码优雅的证明自己 NB
console.log('([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]',([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]);
}
function test3(){
// 如何优雅的取随机字符串
console.log('Math.random().toString(16).substring(2)',Math.random().toString(16).substring(2));
console.log('Math.random().toString(36).substring(2)',Math.random().toString(36).substring(2));
}
function test4(){
// 如何优雅的取整
var a = ~~2.33 // 这种方法还可以将字符串转换成数字类型
var b= 2.33 | 0
var c= 2.33 >> 0
console.log('var a = ~~2.33 var b= 2.33 | 0 var c= 2.33 >> 0',a, b, c);
}
function test5(){
// 如何优雅的实现金钱格式化: 1234567890 -> 1,234,567,890
var test1 = '1234567890'
var format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
console.log('var test1 = 1234567890 var format = test1.replace(/\B(?=(\d{3})+(?!\d))/g,',')',format) // 1,234,567,890
}
// 逗号运算符
function test6(){
var a = 0;
var b = ( a++, 99 );
console.log('var a = 0;',a); // 1
console.log('var b = ( a++, 99 );',b); // 99
}
// 论如何最佳的让两个整数交换数值
function test7 (){
var a = 0;
var b = ( a++, 99 );
a ^= b;
b ^= a;
a ^= b;
console.log ('var a = 0; var b = ( a++, 99 ); a ^= b; b ^= a; a ^= b;',a,b);
}
// 实现标准 JSON 的深拷贝
function test8(){
var a = {
a: 1,
b: { c: 1, d: 2 }
}
var b=JSON.parse(JSON.stringify(a))
console.log('var a = {a: 1, b: { c: 1, d: 2 } } var b=JSON.parse(JSON.stringify(a))',b);
}
// 不用 Number,parseInt 和 parseFloat 和方法把 "1" 字符串转换成数字
function test9(){
var a =1 ;
+a
console.log('var a =1 ; +a',typeof(a));
}
function test10(){
console.log('parseInt(0.0000008) === 8',parseInt(0.0000008) === 8)
console.log('++[[]][+[]]+[+[]] == 10',++[[]][+[]]+[+[]] == 10)
console.log('0.1 + 0.2 == 0.3',0.1 + 0.2 == 0.3)
}
// 0.1 +0.2 == 0.3 竟然是不成立的.... 所以这就是为什么数据库存储对于货币的最小单位都是分.
// 简单说, 0.1 和 0.2 的二进制浮点表示都不是精确的, 所以相加后不是 0.3, 接近 (不等于)
// 0.30000000000000004.
// 所以, 比较数字时, 应该有个宽容值. ES6 中这个宽容值被预定义了: Number.EPSILON.
// 最短的代码实现数组去重
function test11(){
console.log('[...new Set([1,"1", 2, 1, 1, 3])]',[...new Set([1, "1", 2, 1, 1, 3])]);
}
// 取出一个数组中的最大值和最小值
function test12(){
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);
console.log('var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; var maxInNumbers = Math.max.apply(Math, numbers); var minInNumbers = Math.min.apply(Math, numbers);',maxInNumbers,minInNumbers);
}
// 这个就厉害了
function test13(){
while (1) {
alert('牛逼你把我关了啊')
}
}
</script>
来源: http://blog.csdn.net/qq_39517820/article/details/79195109