Variable objects in javascript

Source: Internet
Author: User

In js, variable objects have various problems of passing values and assigning values. Next I will introduce some issues about javascript variable objects to you. You can refer to them for reference.

I saw a piece of code on the Internet today, as shown below:

The Code is as follows: Copy code

Copytext
Console. log (typeof (s); // function
Var s = "1111 ";
Function s (){}
Console. log (typeof (s); // stringconsole. log (typeof (s); // function
Var s = "1111 ";
Function s (){}
Console. log (typeof (s); // string


After a long time, I finally figured out the variable object in JS.
The first line outputs the function, because after entering the scope, the variable object will first scan the function of the scope of course, and then scan the variable, then store the variable object, and then start to execute the code.

Therefore, the first line outputs the function.

The fourth line stores the variable object before the variable, so the second line of code s = "1111" overwrites the original function variable s, so the output string


Question about passing variable values into an object

The problem is as follows:

The Code is as follows: Copy code
Var o = 'left ';
Var o1 = {drt: 'left '};
Var o2 = ['left'];

Var obj = {o: 333 };
Var obj1 = {o1 [drt]: 333 };
Var obj2 = {o2 [0]: 333 };

The first three lines of the Code are several different declaration methods of variables, including common variables, objects, and arrays. The last three lines call these three variables and insert objects. Question:
1. Why is an error reported directly when obj1 and obj2 are declared?
2. No error is reported for obj, but why is the first attribute called "o" rather than the value of variable o "left" When looping its attributes?

If I want to pass a variable to an object as a property, and pass the variable value to the object as a property value, is it only used:

The Code is as follows: Copy code

Var o1 = {drt: 'left '};
Var obj3 = {drt: o1.drt}


Variable assignment in class

The difference between object. prop and object [prop] assignment in Javascript.

To give a simple example, for example, you need to obtain the information of the open shard from stocks to form an openList object. The implementation code is as follows:

The Code is as follows: Copy code
Var stocks = {
'000000': {'name': 'pudong Development Bank ', 'open': '9. 67', 'new': '9. 54 '},
'000000': {'name': 'baiyun Airport ', 'open': '6. 82', 'new': '6. 77 '},
'000000': {'name': 'steel & Steel stock ', 'open': '3. 23', 'new': '3. 19 '},
'000000': {'name': 'dongfeng Motors', 'open': '3. 96 ', 'new': '3. 86 '}
};

Var openList = {
'123': '9. 67 ',
'123': '6. 82 ',
'123': '3. 23 ',
'000000': '3. 96'
};

Var result = {};
Var I;
// Obtain the data object of the open shard
For (I in stocks ){
// Method 1
// Result [I] = stocks [I] ['open'];
// Method 2
Result. I = stocks [I] ['open'];
}

Method 1 can obtain the expected result.

But the result obtained in method 2 is {I: "3.96 ″}.

Therefore, the object. attribute cannot be a variable. For example, I is overwritten by an attribute of the object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.