JavaScript Study Notes

Source: Internet
Author: User

JavaScript Study Notes

1. During comparison, try to use "=" instead of "=", because the "=" operator will force type conversion to compare two values, this will cause performance consumption.


2. To achieve the best performance of traversing arrays, we recommend that you use a classic for loop.
Var list = [1, 2, 3, 4, 5, ....... 100000000];
For (var I = 0, l = list. length; I <l; I ++ ){
Console. log (list [I]);
}
The preceding Code caches the length of an array through l = list. length.
Although length is an attribute of an array, accessing it in each loop still has performance overhead. The latest JavaScript Engine may have been optimized at this point, but we cannot ensure that our code runs on these recent engines.
In fact, the method of not using the cached array length is much slower than that of the cached version.


3. Use var array = [] to declare an array. The code is easier to handle and higher, reducing possible errors.


4. Implicit global variables
// Script
Foo = '42 ';
// Script B
Var foo = '42'
The above two scripts have different effects. Script A defines the variable foo in the global scope, while script B defines the variable foo in the current scope. Therefore, you must use var when declaring variables to prevent inexplicable bugs.


5. Each function in JavaScript can access a special variable arguments. This variable maintains the list of all parameters passed to this function. Note: arguments has been defined as a variable in the function. Therefore, defining arguments by using the var keyword or declaring arguments as a formal parameter will not create native arguments. The arguments variable is not an Array ). Although it has an Array-related attribute length in syntax, it does not inherit from Array. prototype, but is actually an object.


6,
Closure means that the current scope can always access variables in the external scope. Because a function is the only structure with its own scope in JavaScript, the creation of a closure depends on the function. It can simulate private variables
Function Counter (start ){
Var count = start;
Return {
Increment: function (){
Count ++;
},


Get: function (){
Return count;
}
}
}


Var foo = Counter (4 );
Foo. increment ();
Foo. get (); // 5
Here, the Counter function returns two closures: The increment function and the get function. Both functions maintain reference to Counter in the external scope, so you can always access the variable count defined in this scope.


7. In order not to use global variables or modularize the program, there is a method in Javascript: use an anonymous package to create a namespace.
(Function (){
// Function to create a namespace
Window. foo = function (){
// Create a closure for a function exposed to the public
};

}) (); // Immediately execute this anonymous Function
Anonymous functions are considered expressions. Therefore, they are first executed to make them available.


(// The function in parentheses is first executed
Function (){}
) // And returns the function object.
() // Call the preceding execution result, that is, the function object.


There are some other methods that call function expressions. For example, the syntax of the two methods below is different, but the effect is the same.
// Two other methods
+ Function (){}();
(Function (){}());


8. Do not omit semicolons. We also advocate placing curly braces and corresponding expressions in one line. For if or else expressions with only one line of code, we should not omit curly braces. These good programming habits can not only mention code consistency, but also prevent the parser from changing the error handling of code behavior. If the Code does not write a semicolon where the semicolon is needed, the parser automatically inserts a semicolon, which may change the code.


9. call is the built-in function method. Usage: obj1.method1. call (obj2, argument1, argument2)
As shown above, the call function is to put the obj1 Method on obj2 for use, and the argument1. these are passed as parameters. The apply function is the same as the call function, but the apply parameter must be passed in the form of an array. Therefore, when the parameter is clear, call is available. If the parameter is not clear, apply is available to the combined arguments. For example

Print. call (window, "back", "light", "foot", "book ");

Print. apply (window, arguments );

Related Article

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.