!!的使用

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6     <script type="text/javascript">
 7         function test(){
 8             var obj={name:"哈喽"};
 9             var t1=!!obj.name;//等效于var test=o.flag||false;
10              console.log(t1);//true
11             var t2=obj.name||false;
12             console.log(t2);//哈喽
13
14             var obj1={name:""};
15             var t3 = obj1.name||false;
16             console.log((!!obj1.name)+""+t3);//false
17
18             var obj2={name:undefined};
19             var t4 = obj2.name||false;
20             console.log((!!obj2.name)+""+t4);//false
21
22             var obj3={name:null};
23             var t5 = obj3.name||false;
24             console.log((!!obj3.name)+""+t5);//false
25
26             var obj4={name:" "};
27             var t6 = obj4.name||false;
28             console.log((!!obj4.name)+""+t6);//true 空格
29
30             var obj5={};
31             console.log(!!obj5.name);//false
32
33             var obj6;
34             console.log(!!obj6);//false
35
36             var obj7=‘‘;
37             console.log(!!obj7);//false
38
39             var obj8=‘ ‘;
40             console.log(!!obj8);//true
41
42             var obj9=undefined;
43             console.log(!!obj9);//false
44
45             var obj10=null;
46             console.log(!!obj10);//false
47         }
48         function isSupportHistoryPushState(){
49             console.log(!!(window.history && history.pushState));//
50             return !!(window.history && history.pushState);//返回true or false
51         }
52     </script>
53 </head>
54 <body>
55 <input type="button" onclick="test();" value="test!!的使用方式"/>
56 <input type="button" onclick="isSupportHistoryPushState();" value="判断浏览器是否支持history.pushState"/>
57 </body>
58 </html>
时间: 2024-10-19 21:48:48