OReilly JavaScript The Good Parts 关于变量的使用

来源:互联网 发布:吃鸡网络延迟检测 编辑:程序博客网 时间:2024/04/29 16:52

When used well, this can reduce object initialization time and memory consumption.

/*The quotes around a property's name in anobject literal are optional if the name would be a legal JavaScript name and not a reserved word*/var stooge={             name:"hebian",            "no.":"001"           };//alert(stooge["name"]);//alert(stooge["no."]);




/*Objects can nest*/

var flight = {              airline: "spring",              number: 123,              departure:{                         time:"09-22 14:55",                         city:"shanghai"                        }             };//alert(flight["airline"]);//alert(flight.airline);//alert(flight.departure.time);


 

/*The || operator can be used to fill in default values:*///alert(flight.arrival || "unknown");/*Attempting to retrieve values from undefined will throw a TypeError exception. This can be guarded against with the && operator:*///alert(flight.arrival && flight.arrival.time);


 

/*If the property name already exists in the object, theproperty value is replaced:*/flight.number += 1;//alert(flight.number);/*If the object does not already have that property name, the object is augmented:*/flight.arrival = {                  time:"09-22 19:55",                  city:"changsha"                 };alert(flight.arrival.city);


 

/*Objects are passed around by reference. They are never copied:*/var x = stooge;x.nickname = 'Curly';var nick = stooge.nickname;// nick is 'Curly' because x and stooge// are references to the same objectvar a = {}, b = {}, c = {};// a, b, and c each refer to a// different empty objecta = b = c = {};// a, b, and c all refer to// the same empty object


 

原创粉丝点击