javscript 中的术语和俚语
语言中俚语和方言。在JavaScript中也有一些俚语或者说是术语,看似奇淫巧技,还是有一些用处,有三种语言组件可以来构造术语:强转、逻辑运算符和位变换。
1、强转:在javascript和大部分的语言中,强转是指强制将一种类型的对象或者实体转成另外一种的行为
1.转成String var d = ' ' + 0; console.log(d); //'0'
2.转成数字 console.log(+'10')//10 console.log(+new Date()) //时间戳 1471493169160 console.log(new Date() + '') //Thu Aug 18 2016 12:06:49 GMT+0800 (中国标准时间)
3.复杂的强转 ++[[]][+[]]+[+[]] //'10' [[]] // =>[Array[0]] [+[]] // =>[0] 就是0 1 + [0] //=>10
2.逻辑运算符
1.通过比较或隐式回退赋值 && 可以避免报错 当赋值语句来用 || 可以用来填加默认值 name = arg [0] || "dfd" ! 转换成布尔值来做捷径 1.console.log(!0) // true 2.console.log(!1) //false 3.console.log(!-1) //false 4.console.log(!'0')//false 5.console.log(!'1')//false 6.console.log(!undefined)//true 7.console.log(!this)//false 8.console.log(!+[]) // true 9.console.log(![][![]])//true 10.console.log(!null)//true
2.双重否 !! 如果是真就输出真,如果是null和undefined就!null 就是false false直接输入false,避免报错。 var user = { isAdmin: function(){ return !! this.admin } } ; console.log(user.isAdmin()) //false !! undefined => !true =>false user.admin = true; console.log(user.isAdmin()) //true !!true => !false =>true user.admin = false; console.log(user.isAdmin()) //false !!false => !true =>false
3.立即调用函数 !function(){console.log('foo')}(); //=> foo
3.位变换
1. & 两个操作数进行按位与操作时,如果两个操作数在某个特定的位置都为1,那么按位与返回1 Hex转换为RGB var color = 0xC0FFEE; console.log((color>>16) & 0xFF) //192 red 2. | 两个操作数进行按位与操作时,如果两个操作数在某个特定的位置任意一个为1,那么按位与返回1 数字截断,用来舍入数字 var x = (30.9 | 0); console.log(x); //=> 30
3. ^ 是一种判断两个操作数是否具有相反符号的简单方法 1、符号判等 var signsMatch = function(x,y){ return !((x ^ y) < 0); }; console.log(signsMatch(10, 10)); //fasle console.log(signsMatch(0, 0)); //true console.log(signsMatch(-10, -10)); //true 2.翻转位 可以用来切换对象的状态是非常有用。 var light = { on:1, toggle: function(){ return this.on ^=1; } } console.log(light.toggle()) //=>0 console.log(light.toggle()) //=>1 console.log(light.toggle()) //=>0
4.~ 非 ,按位非从功能上说,基本是讲一个数字的符号进行反转,然后在减1. 1.按位运算 ~-10 // =>9 -~10 //=>11 2*~-10 // =>18 2.字符串转数字 var num = “100.7” console.log(parseInt(num,10) === ~~num) //true
4.一些难懂的代码
1、暗中的eval ““[”sub”]["constructor"](""console.log('foo')")() //=>foo
2.进制 当比较不同进制的数字是要保持小心。比如比较八进制和十进制和科学计数 1+ 064 == 65 //false 064 > 60 //false 30000000>4e9 //false 3.Unicode编码的变量 在javascript中允许使用Unicode用作属性描述符和变量名, var \u1000 = {\u1001: function(){ return 'Unicode' } } console.log(\u1000.\u1001()) //'Unicode'
false => ![] true => !![] undefined => [][[]] NaN => +[![]] 0 => +[] 1 => +!+[] 2 => !+[]+!+[] 10 => [+!+[]]+[+[]] Array => [] Number => +[] String => []+[] Boolean => ![] Function => []["filter"] eval => []["filter"]["constructor"]( CODE )() window => []["filter"]["constructor"]("return this")()
[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()
上面的代码是 alert(2)
具体的网站可以访问:http://www.jsfuck.com/