javascript---the advantage of accessing the properties of object as array(objects as associative arr...

本文探讨了JavaScript中使用.操作符与[]操作符访问对象属性的不同之处。.操作符适用于已知且固定的属性名,而[]操作符则能够动态地获取属性,适用于运行时未知的属性名情况。

in javascript,we often use . operator to access the properties of an object like followings:

 


  
var obj = new myObj(); // initialize the obj with new
var myName = myobj.myName; // access the property with . operator

 

on the other hand,we can also access the properties of objects by [] operator just like accessiing the array;

 

 

 


  
var obj = new myObj(); // initialize the obj with new
var myName = myobj[ " myName " ]; // access the property with [] operator

 

what' is the difference between them?

in the first style,we access the property by the property name,the property name is a identifier.you know we can't change the property name,in other word ,we can't manipulate the identifier by program.

in the second,the property name is a string ,it is most important, it means we can access the properties of objects dynamicly.the property name can be manipulated and created while a program is running.

just seeing some example code:

 


  
var names;
for ( var i = 0 ;i < 4 ;i ++ ) // a loop
{
names
+= alex[ " name " + i] + ' \n ' ; // set the property dynamicly
}

 

in other situation,if the property name and corresponding value is not defined in the code,but comes from the input of the user,absolutely, we can't access the property and assign the value to it whith identifier of property name, because it doesn't exist. in this case,we can also use the [] operator to access the property and assign the value to it,example code is just as following:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码

   
var inputValue = GetInputValueFromUser(); // get value
var inputProperty = GetInputPropertyFromUser(); // get property
obj[inputProperty] = inputValue; // it has done

// return the corresponding value
function GetInputValueFromUser()
{
return " testValue " ;
}

// /return the dynamic property
function GetInputPropertyFromUser()
{
return " testProperty " ;
}

 

since the user enters the property  at runtime.there is no doubt we can't know the property name ahead of time and also when we write the program.so there is no way to use . operator to access the property of the object.howere we can use [] operator with string value(it  can be changed  dynamicly at runtime).

so this is the advantage of using [] operator to access the properties of a object.

but if someone wants to  access the properties of object and doesn't use the [] operator,how should we do?don't worry,there is a way which can help us,we can use eval function ,which can make js complier treat a string as a js code,some example just seehttp://www.cnblogs.com/heaiping/archive/2010/06/16/1757962.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值