Home  >  Article  >  Web Front-end  >  In-depth understanding of JavaScript study notes (1) Writing high-quality code_javascript skills

In-depth understanding of JavaScript study notes (1) Writing high-quality code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:51:05979browse

1. Variables

•Global variables
Two features of JavaScript make it surprisingly easy to create global variables unconsciously. First, you can use variables without even declaring them; second, JavaScript has an implicit global concept, which means that any variable you do not declare will become a global object property (not a true global variable, you can use delete Delete)

Copy code The code is as follows:

function sum(x,y) {
// result is not declared, it is an implicit global variable
result = x y;
return result;
}

function foo() {
// Use task chain for part var declaration, b is an implicit global variable
var a = b = 1;
}

Suggestion:
Copy Code The code is as follows:

function (x,y) {
var a ,b ;
a = b = 1;//a, b is a local variable
}

•var function
Global variables created through var (created in any program other than functions) cannot be deleted. Implicit global variables created without var (regardless of whether they are created in a function) can be deleted.

Copy code The code is as follows:

// Define three global variables
var global_var = 1;
global_novar = 2; // Negative teaching material
(function () {
global_fromfunc = 3; // Negative teaching material
}());

// Attempt to delete
delete global_var; // false
delete global_novar; // true
delete global_fromfunc; // true

// Test the delete
typeof global_var; // "number"
typeof global_novar; // "undefined"
typeof global_fromfunc; // "undefined"

•Single var declaration of variables
Use a single var statement at the top of the function It is a more useful form. The initial value of all uninitialized but declared variables is undefined

Copy code The code is as follows:

function func() {
var a = 1,
b = 2,
sum = a b,
myobject = {},
i,
j;
// function body...
}

•var spreading problem
Copy code The code is as follows:

//Counter example
myname = "global"; // Global variable
function func() {
alert(myname); //"undefined" var myname = "local"; alert(myname); // "local" } func(); is equivalent to:


myname = "global"; // global variable
function func( ) {
var myname; // Equivalent to -> var myname = undefined;
alert(myname); // "undefined"
myname = "local";
alert(myname); // "local"}
func();

Two for loops

• It is recommended to use
to copy Code The code is as follows:

function looper() {
var i = 0,
max,
myarray = [];
// ...
for (i = 0, max = myarray.length; i < max; i ) {
// Use myarray[i] to do something
}
}

Replace i with the following expression
Copy the code The code is as follows:

i = i 1
i = 1 The following two looping methods are faster


//The first changed form:

var i, myarray = [ ];
for (i = myarray.length; i–-;) {
// Use myarray[i] to do something
}

//The second one uses a while loop :

var myarray = [],
i = myarray.length;
while (i–-) {
// Do something with myarray[i]
}

•for-in loop
Apply to traversal of non-array objects. Arrays use normal for loops, and objects use for-in loops. Using the hasOwnProperty() method, you can filter out properties that come down the prototype chain when traversing object properties.

3 Avoid implicit type conversions

• Stick to === and! ==

4. Avoid using eval, and avoid passing strings to setInterval(), setTimeout() and Function() constructors, use functions instead.

Five parseInt() numerical conversion

It is recommended to assign a value to the base parameter,
Copy code The code is as follows:

var month = "06",
year = "09";
month = parseInt(month, 10);//Strings starting with 0 will be Treated as octal
year = parseInt(year, 10);

Six programming standards

Constructor naming: MyConstructor();

General Function naming: myFunction();

Variable naming: firstName;

Private properties or methods: _secondeName,

Constants: PI, MAX;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn