Fork me on GitHub

反射,减少对全局变量的污染

 1 <script type="text/javascript">
 2     /*反射*/
 3     typeof flight.number     //'number'
 4     typeof flight.status    //'string'
 5     typeof flight.arrival    //'object'
 6     typeof flight.manifest    //'undefined'
 7 
 8     typeof flight.toString     //'function'
 9     typeof flight.constructor//'function'
10 
11     flight.hasOwnProperty('number')        //true
12     flight.hasOwnProperty('constructor')//false
13 
14     /*减少全局变量的污染*/
15     全局变量削弱了程序的灵活性,尽量避免
16     var Mapp={};
17     Mapp.stooge={
18         "first-name":"Joe",
19         "last-name":"Howard"
20     };
21     Mapp.flight={
22         airline:"Oceanic",
23         number:815,
24         departure:{
25             IATA:"SYD",
26             time:"2004-09-22 14:55",
27             city:"Sydney"
28         },
29         arrival:{
30             IATA:"LAX",
31             time:"2004-09-23 10:42",
32             city:"Los Angeles"
33         }
34     };
35     </script> 

 

posted @ 2015-07-29 17:18  Terry√  阅读(260)  评论(0编辑  收藏  举报