Order
I did not appear in the recommendation column for a long time, it seems that a good response, so the continuation of the article.
Properties
? When accessing properties, we use the dot (.) operator.
var Luke = {
Jedi:true,
Age:28
};
Bad
var Isjedi = luke[' Jedi '];
Good
var isjedi = Luke.jedi;
? When accessing a property as a variable, use the subscript symbol ([]). -Avoid the use of obj[variable as a way to access property unless you have special needs.
var Luke = {
Jedi:true,
Age:28
};
function Getprop (prop) {
return Luke[prop];
}
var Isjedi = Getprop (' Jedi ');
Variables
? Always use var to define a variable, or it can cause an implied global variable to be generated. We should try to avoid polluting the global variable namespace.
Bad
Superpower = new superpower ();
Good
var superpower = new superpower ();
Uncle Tom-javascript series of articles mentions that "JavaScript has an implied global concept, meaning that any variable you do not declare will become a global object attribute." In technology, implicit global variables are not true global variables, but they are properties of global objects. Properties can be deleted by the delete operator, and the variable is not. "
? Use a var to define multiple variables, each variable on a new line.
Bad
var items = GetItems ();
var gosportsteam = true;
var dragonball = ' Z ';
Good
var items = GetItems (),
Gosportsteam = True,
Dragonball = ' Z ';
? When you define multiple variables with VAR, placing variables that are not assigned to the end--this is quite beneficial. Especially if your variable needs to assign the value of the preceding variable.
Bad
var i, Len, Dragonball,
Items = GetItems (),
Gosportsteam = true;
Bad
var i, items = GetItems (),
Dragonball,
Gosportsteam = True,
Len
Good
var items = GetItems (),
Gosportsteam = True,
Dragonball,
Length
I
? Place your assignment variable at the top of the current scope. This avoids the problem of variable declarations and hoisting (mount/top parsing/Pre-resolution).
Bad
function () {
Test ();
Console.log (' doing stuff ... ');
//.. Other stuff.
var name = GetName ();
if (name = = = ' Test ') {
return false;
}
return name;
}
Good
function () {
var name = GetName ();
Test ();
Console.log (' doing stuff ... ');
//.. Other stuff.
if (name = = = ' Test ') {
return false;
}
return name;
}
Bad
function () {
var name = GetName ();
if (!arguments.length) {
return false;
}
return true;
}
Good
function () {
if (!arguments.length) {
return false;
}
var name = GetName ();
return true;
}
Hoisting
Uncle Tom:
1, in JavaScript, you can declare multiple var statements anywhere in a function, and they act as if they were declared at the top of a function, which is called hoisting (mount/sticky parsing/pre-resolution).
2, for JavaScript, as long as your variable is in the same scope (the same function), it is declared, even if it is used before the Var declaration.
? Variable declarations are promoted to the top of the current scope, and their assignment operations do not necessarily do so.
function Example () {
Console.log (notdefined); => throws a referenceerror this article link http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130709/39006.html
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.