The distinction between naming and variables as object attributes when declaring variables _javascript tips

Source: Internet
Author: User

The title is a mouthful, JavaScript. The rules followed by named variables

1. The first character must be a letter, Chinese character, underscore (_) or dollar sign ($)

2, the rest can be underlined, Chinese characters, dollar sign and any letter, number

The following declaration variables are correct

Copy Code code as follows:

var p, $p, _p;
var long, wide;

Here's the wrong
Copy Code code as follows:

var. p;//can only be letters, numbers, underscores, or dollar signs
var-p;//can only be letters, numbers, underscores, or dollar signs
var p*;//can only be letters, numbers, underscores, or dollar signs
var 4p,4 long;//cannot start with a number
var length;//no spaces in the middle

As an object property, there are two ways of accessing it. One is the dot number (.) operator, one is the bracket ([]) operator.
Copy Code code as follows:

var p = {name: "Jack"};
alert (p.name);//point number
Alert (p[' name ');//Bracket

1, the dot number required after the operator is a valid identifier (that is, the legal variable naming), for illegal not to use

2, the brackets required is a string, you do not have to be a legitimate variable naming. such as 4p is an illegal variable name (because it starts with a number), but it can be an object property name (provided it is a string)

Copy Code code as follows:

var p = {
"4p": "Jack",
"-3": "Hello",
Name: "Tom",
"Me": "Me",
"We": "We"
};

alert (p.4p);/illegal, grammatical analysis times wrong, can't start with numbers
Alert (P. Me);//Legal, Output "me"
Alert (p. us);/illegal, grammatical analysis times wrong ("I" and "we" have a space between)
Alert (p["we"));/Legal, output "we", although there are spaces between "I" and "we", can still be accessed using []
Alert (p[' 4p ']);/legal, output "Jack"
alert (p.name);/Legal, output "Tom"

When declaring an object variable with a direct amount, the property name is sometimes quoted and sometimes not, but the object's property type is string regardless of whether it is added or not.
Copy Code code as follows:

var book = {bname: "JS authoritative guide", "price": 108};//bname without quotes, Price added
for (var attr in book) {
Two times output is string, stating that JS will dynamically convert it to a string type
Alert (attr + ":" + typeof (attr));
}

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.