您的位置:首页 > 其它

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

2015-07-29 17:18 375 查看
<script type="text/javascript">
/*反射*/
typeof flight.number     //'number'
typeof flight.status    //'string'
typeof flight.arrival    //'object'
typeof flight.manifest    //'undefined'

typeof flight.toString     //'function'
typeof flight.constructor//'function'

flight.hasOwnProperty('number')        //true
flight.hasOwnProperty('constructor')//false

/*减少全局变量的污染*/
全局变量削弱了程序的灵活性,尽量避免
var Mapp={};
Mapp.stooge={
"first-name":"Joe",
"last-name":"Howard"
};
Mapp.flight={
airline:"Oceanic",
number:815,
departure:{
IATA:"SYD",
time:"2004-09-22 14:55",
city:"Sydney"
},
arrival:{
IATA:"LAX",
time:"2004-09-23 10:42",
city:"Los Angeles"
}
};
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: