JavaScript High Quality Coding Essentials Note __javascript

Source: Internet
Author: User

The original text comes from the basics of writing high quality JavaScript code, which is the global variables issue for the sorted notes

var a = 5;
function func () {
    b = 6;  Do not recommend
}
Variables that are created with VAR using var declaration cannot be removed without using a variable that is not declared, and the directly used global variable can be delete

The single var form provides a single place to look for all the local variables needed for the function

var a = 1,
    b = 2,
    sum = a + b,
    myobject = {},
    I,
    J;

Variable elevation problem, the variable declaration is raised to the top

var a =;
function func () {
    console.log (a);
    var a = ten;
}
Func ();  Output undefined
//The execution order of the above statement
var A;  Global variable declaration
a =;
function func () {
    var A;  Local variable declaration
    Console.log (a);  Print local variable a
    = ten;
}
Func ();

A function declaration takes precedence over a variable declaration, but if the variable is assigned, it overrides the function declaration for Loop Problem

Caching the length of an array (or collection) before the loop is a better form

var i, max, myarray = [' Hello '];
for (i = 0, max = Myarray.length i < max; i++) {
   console.log (myarray[i));

Down to 0, usually faster, because comparing to 0 is more efficient than an array of lengths or something other than 0, and the max variable is less

var i, myarray = [' Hello '];
for (i = myarray.length; i--;) {
   console.log (myarray[i]);
}

For in loop object, you can use the hasOwnProperty () method to filter properties from the prototype chain

var man = {
   hands:2,
   legs:2,
   heads:1,
};
Object.prototype.clone = function () {} for
(var i in mans) {
   if (Man.hasownproperty (i)) {//filter
      Console.log ( I, ":", Man[i]);
   }
other

Avoidance of implicit type conversions

var zero = 0;
if (zero = = False) {/
   /not executed because zero is 0 instead of false
}

//Reverse Example
if (zero = false) {
   //execute ...
}

Avoid using eval ()

Passing strings to SetInterval (), settimeout (), and function () constructors, in most cases, is similar to using eval (), so avoid it.

Reverse Example
settimeout ("MyFunc ()", 1000);
SetTimeout ("MyFunc (1, 2, 3)", 1000);

Better
settimeout (MyFunc, 1000);
settimeout (function () {
   myFunc (1, 2, 3);
}, 1000);

parseint (), in ECMAScript 3, the string beginning with "0″ is treated as 8, but this has changed in ECMAScript 5, always specifying cardinality parameters to avoid conflicting and unexpected results

parseint ("a");  6
parseint ("06hello");  6
parseint ("hello06");  NaN
//The following method is quicker to handle, but "06hello" string can only be resolved with parseint
+ "a";  6
+ "06hello";  NaN number
("a");  6 number
("06hello");  NaN
Naming ConventionsFor constructors, you can use the Big hump nomenclature (upper camel case), such as myconstructor () for function and method names, you can use the small hump notation, such as myfunction () other variables using the underline naming method, such as My_arr Constants are underlined using all the words in uppercase, such as Max_width = 800; Private members use a front underline, such as _privatekey Space Specification For loop semicolon separate part: As for (var i = 0; i < i = 1) {...} for the variable initialized in the For loop (I and Max): for (var i = 0, max = i < max; i = = 1) {...} separates the comma after the array entry: var a = [1, 2, 3]; After the object property comma and after the colon separating the property name and property value: var o = {a:1, b:2}; Qualifying function Arguments: MyFunc (A, B, c) the front of the curly braces for the function declaration: the function MyFunc () {} After the anonymous function expression function: var MyFunc = function () {};

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.