TIP: The content is extracted from the JavaScript authoritative guide, and students who have read the book can ignore this article.
How to access the properties of an object:
obj.attr;
obj["attr"];
The most important difference between the two is that the former property name is the identifier , and the latter's property name is a string .
Use "." operator to access the properties of an object, property names are identifiers, and in JavaScript programs, identifiers must be entered verbatim, they are not a data type, so the program cannot manipulate them.
When you use the concept "[]" of an array to access the properties of an object, the property name is represented by a string. Strings are a data type of JavaScript, so you can manipulate and create them while the program is running.
Associative arrays
obj["attr"];
If the use of an object is in this form, we often call it an associative array. Associative arrays are data structures that allow you to dynamically associate arbitrary values with arbitrary strings. In fact, the internal implementation of a JavaScript object is an associative array.
Application Scenarios (obj["attr"])
When you need to write a program that allows Yonghu to enter the name of the stock he owns and the corresponding share to calculate the current value of the user's investment in the stock market, you can do so.
Use a Portfolio object to hold this information, the name of each stock entered by the user as a property of the object, the value of which is the share of the stock. For example: The user owns Netscape Company's stock, the share is 50, there is PORTFOLIO.NSCP = 50;
This program requires a loop, first prompting the user to enter a stock name, and then entering the share he has.
Because the user enters the stock name (the attributes of the Portfolio object) while the program is running, you cannot know in advance what these stock names are, so you cannot use "." When programming. operator, but you can use "[]" to associate an array, because his property name is a string that can be created dynamically when the program is run.
Here is the specific code
(function(){varPortfolio = {};varname,value,sum=0;//define stock name/share/investment current value (total amount) while(true){//Enter the stock name and share and write to the Portfolio objectif(! (name = prompt ("Please enter a stock name", "NSCP") | |false)) Break;if(! (Value= prompt ("Please enter your share", 50) | |false)) Break;p Ortfolio[name]= +value;} for(EinchPortfolio) {//Traverse Calculated Investment Current value (total amount)Sum +=portfolio[e]*1;//assuming each stock is 1 yuan, each stock quantity * stock price}console.log (sum);//Output})();
Because the stock name is unknown, if there is no for/in loop, we cannot write this code, output the property name and calculate the result. This is the only way to extract the property name from the Portfolio object
Attention:
For (e in portfolio) { sum +=portfolio[e]*1;}
, E gets only the Portfolio property name, not the attribute value corresponding to the property name, so "portfolio[e" is necessary
In addition, you can also read this article, the content is almost [import]js associative array
An associative array of JavaScript objects