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));
}