您的位置:首页 > Web前端 > JavaScript

OReilly JavaScript The Good Parts 关于变量Property的访问

2012-12-11 08:49 633 查看
var flight = {
airline: "spring",
number: 123,
departure:{
time:"09-22 14:55",
city:"shanghai"
}
};
document.writeln(typeof flight);
document.writeln(typeof flight.airline);
document.writeln(typeof flight.number);
document.writeln(typeof flight.toString);

document.writeln(flight.hasOwnProperty('number'));
document.writeln(flight.hasOwnProperty('arrival'));

/*result
object string number function true false
*/

for (name in flight)
{
if (typeof flight.name != 'function')
{
document.write(name + ':' + flight[name]);
}
//    document.write(typeof name);
}

/*There is no guarantee on the order of the names,
If you want to assure that the properties appear
in a particular order,
*/

var properties = [
'airline',
'number'
];
for (i = 0 ; i < properties.length; i++)
{
document.writeln(properties[i] + ':' + flight[properties[i]]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: