1、javascript中的双感叹号语法(!!)
var o={flag1:true};
var test1=!!o.flag1;//等效于var test1=o.flag1||false;
console.log(test);//true
var test2=!!o.flag2;//等效于var test2=o.flag2||false;
console.log(test);//false 而不是undefined 或 null
由于对null与undefined用 !! 操作符时都会产生true的结果,
所以用两个感叹号的作用就在于,
如果设置了o中flag的值(非 null/undefined/0”“/等值),自然test就会取跟o.flag一样的值;
如果没有设置,test就会默认为false,而不是 null或undefined。
————————————————
2、javascript中加号(+)操作符的作用
// 16进制转换:
+”0xFF”; // -> 255
// 获取当前的时间戳,相当于`new Date().getTime()`:
+new Date();
// 比 parseFloat()/parseInt()更加安全的解析字符串
parseInt(“1,000″); // -> 1, not 1000
+”1,000″; // -> NaN, much better for testing user input
parseInt(“010″); // -> 8, because of the octal literal prefix
+”010″; // -> 10, `Number()` doesn't parse octal literals
//一些简单的缩写比如: if (someVar === null) {someVar = 0};
+null; // -> 0;
// 布尔型转换为整型
+true; // -> 1;
+false; // -> 0;
//其他:
+”1e10″; // -> 10000000000
+”1e-4″; // -> 0.0001
+”-12″; // -> -12: