How to access the properties of an object in Javascript

Javascript has three different kinds of properties: named data property, named accessor property and internal property. There are two kinds of access for named properties: get and put, corresponding to retrieval and assignment, respectively. Please refer to the Ecma-262.pdf.javascript

Here are the ways of accessing named data property.java

1 var obj = {
2 
3     property_1: 123
4 
5 };

You can get its value like the following:ui

1 obj.property_1;

Orspa

obj[「property_1」]; //please note, quote is required here

but you have to use [「xxx」] if you intend to use for/in to access the properties of an objectcode

1 for(p in obj)
2 {
3     alert(obj[p]);
4 }

The system will alert undefined if you access like the following within the for/in, the reason is javascript pass the property as stringblog

for (p in obj)
{
    alert(obj.p);
}
相關文章
相關標籤/搜索