Problems with variable objects in JavaScript

Source: Internet
Author: User

Today I saw a piece of code online, as follows

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


Pondering for a long time, finally want to understand the problem of JS variable object
The first line outputs a function, because when the scope is entered, the variable object first sweeps through the function of the scope, and then sweeps a variable, then the variable object, and then starts executing the code.

So the first line outputs the function.

and line four because the function is in the variable object before the variable, so in the second line of code s = "1111″, it overwrites the original function variable s, so output string


problem when passing a variable value into an object

The questions are 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 code are several different ways of declaring a variable, that is, a normal variable, an object, an array, and the last three rows that call each of these 3 variables and insert the object. Here's the problem:
1. Why Obj1 and OBJ2 statements when the direct error?
2. obj did not complain, but why loop his properties when the first attribute is directly called "O" instead of the value "left" of the variable O?

If I want to pass the variable to the object when the attribute, the value of the variable to the object when the property value, only use:

The code is as follows Copy Code

var O1 = {drt: ' Left '};
var OBJ3={DRT:O1.DRT}


assignment problems of variables in a class

The difference between Object.prop and Object[prop] assignments in JavaScript.

Give a simple example, such as to get the opening price from the stocks information, to form a Openlist object. The implementation code is as follows

The code is as follows Copy Code
var stocks = {
' 600000 ': {' name ': ' Pu FA Bank ', ' open ': ' 9.67 ', ' new ': ' 9.54 '},
' 600004 ': {' name ': ' Baiyun airport ', ' open ': ' 6.82 ', ' new ': ' 6.77 '},
' 600005 ': {' name ': ' Wisco shares ', ' open ': ' 3.23 ', ' new ': ' 3.19 '},
' 600006 ': {' name ': ' Dongfeng Motor ', ' open ': ' 3.96 ', ' new ': ' 3.86 '}
};

var openlist = {
' 600000 ': ' 9.67 ',
' 600004 ': ' 6.82 ',
' 600005 ': ' 3.23 ',
' 600006 ': ' 3.96 '
};

var result = {};
var i;
Get the open Price data object
For (i in stocks) {
Method 1
Result[i] = stocks[i][' open '];
Method 2
RESULT.I = stocks[i][' open '];
}

Here's Method 1, to get the desired result.

But Method 2 Gets the result: {i: "3.96″}."

So the object. Attribute, this attribute cannot be a variable, as I here is an attribute of the object being overwritten.

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.