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

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

/*
The quotes around a property's name in an
object 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, the
property 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 object
var a = {}, b = {}, c = {};
// a, b, and c each refer to a
// different empty object
a = b = c = {};
// a, b, and c all refer to
// the same empty object


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值