OK, no more nonsense, and then the last article.
Variable (variables)
Always use the var keyword to define the variable, assuming that this will cause the variable to be global, causing pollution.
Bad
SuperPower = new SuperPower ();
Good
var superPower = new SuperPower ();
Use a Varkeyword to define multiple variables ... And one row for each variable:
Bad
var items = GetItems ();
var gosportsteam = true;
var dragonball = ' Z ';
Good
var items = GetItems (),
Gosportsteam = True,
Dragonball = ' Z ';
I personally prefer the following definition methods.
var items = GetItems ()
, Gosportsteam = True
, Dragonball = ' Z ';
Place unassigned variables behind ...
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
The definition and assignment of variables are placed at the top of the scope, which avoids problems related to variable elevation.
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;
}
The above example I feel so wonderful ... This name does not play any role at all ...
Hoisting (domestic book translation for promotion)
The definition of the variable will be lifted, that is, moved to the top of the scope, but the assignment is not promoted.
Give a simple example: For simple var a = 10;
Actually, it's going through two processes,
①var A;
②a = 10;
Well, the variable boost is just the first part of Ascension, and the second part is not improved ...
function Example () {
Console.log (declaredbutnotassigned); = undefined
var declaredbutnotassigned = true;
}
function Example () {
var declaredbutnotassigned;
Console.log (declaredbutnotassigned); = undefined
Declaredbutnotassigned = true;
}
An anonymous function expression promotes only the name of the variable, not the definition of the function.
function Example () {
Console.log (anonymous); = undefined
Anonymous (); = TypeError Anonymous is not a function
var anonymous = function () {
Console.log (' anonymous function expression ');
};
}
A named function is promoted by the same name as the variable, not the name of the function. or the function body.
function Example () {
Console.log (named); = = undefined//variable name is improved.
Named (); = = TypeError named is not a function//function body has not been promoted.
SuperPower (); = = Referenceerror SuperPower is not defined//function name has not been promoted.
var named = function SuperPower () {
Console.log (' Flying ');
};
}
function definitions promote their names and function bodies at the same time.
function Example () {
SuperPower (); = Flying
function SuperPower () {
Console.log (' Flying ');
}
}
That's why we have access to this way at the bottom of our code, but we've defined the reason why the function can't be interviewed in the way it was before.
Now you are clear.
Contional Expressions & Equality conditional expressions and equations:
The conditional expression returns a Boolean value by evaluating the content inside the parentheses through the type conversion:
The conversion follows the following rules:
1, Object returns True
2,underfined return False
3, Null returns false
4, the Booleans type is returned to itself.
5, +0,0,-0,nan returns FALSE, other returns true
6, null character returns false: Note that if you have spaces inside your string, the same will return true
if ([0]) {
True
This array is what we mentioned earlier, which is the creation of arrays by literal means .... Of course return true, do not be fooled by 0.
}
Bad
if (name!== ") {
..... stuff ...
}
Good
if (name) {
..... stuff ...
}
Bad
if (Collection.length > 0) {
..... stuff ...
}
Good
if (collection.length) {
..... stuff ...
}
Code block:
Bad
if (test)
return false;
Good
if (test) return false;
Good
if (test) {
return false;
}
Bad
function () {return false;}
Good
function () {
return false;
}
Bad
var x=y+5;
Good
var x = y + 5;
Javascript correct usage Two