[JavaScript] Access the properties of an object
2011-09-04 13:36
603 查看
1. object does not have the property of length, so if you need to iterate properties of the object, you can only
<html> <head> <script type="text/javascript"> function iterateObjectProperty() { var homer = {"name": "Homer Simpson", "age": 34, "married": true, "occupation": "plant operator", 'email': "homer@example.com", x: 1, y: '2' }; var displayValue = ''; var tmp = ''; for(tmp in homer){ displayValue += tmp +": "+ homer[tmp]+", "; } alert(displayValue); } </script> </head> <body> <form> <input type="button" name="demojavascript" value="Demo Iterate Object Property" onclick="iterateObjectProperty();"/> </form> </body> </html>
2. To access the properties of an object, you can
object.property // the property name is an identifier, customer.("address" + i),errorobject[“property”] // the property name is a string, customer["address"
+ i],OK
When you use the . operator to access a property of an object, however, the name of the property is expressed as an identifier.
Identifiers must be typed literally into your JavaScript program; they are not a datatype, so they cannot be manipulated by the program.
The following code is illegal:
var addr = ""; for(i = 0; i < 4; i++) { addr += customer.("address" + i) + '\n'; //error } |
datatypes, so they can be manipulated and created while a program is running. The following code is legal:
var addr = ""; for(i = 0; i < 4; i++) { addr += customer["address" + i] + '\n';// OK } |
相关文章推荐
- How to access the properties of an object in Javascript
- How to get the published properties of an persistent object
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- Javascript Object Method Properties console.log View all methods and properties of the object
- How do I get the name of an object's type in JavaScript
- javascript---the advantage of accessing the properties of object as array(objects as associative arrays)
- How to list the properties of a JavaScript object
- Incorrect decrement of the reference count of an object that is not owned at this point by the caller1
- The Solution of "The calling thread cannot access this object beacuse a different thread owns it."
- How one VBA project can use an object declared in the class module of another project
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Decodes a String into an object of the specified type
- Axis--An implementation of SOAP(Simple Object Access Protocol)
- Amber is an implementation of the Smalltalk language that runs on top of the JavaScript runtime.
- 跟踪算法基准--Tracking the Trackers: An Analysis of the State of the Art in Multiple Object Tracking
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Object Model of JavaScript(http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Details_of_the_Object_Model)
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
- git push failed Because the Fault of insufficient permission for adding an object
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox