Basic javascript knowledge (use code to speak)

Source: Internet
Author: User
Tags hasownproperty

1. js execution sequence
Code:
Myname = "global"; // global variable
Function func (){
Alert (myname); // "undefined"
Var myname = "local"; alert (myname );
Alert (myname); // "local"
}
Func ();
Say:
The js compiler executes the declaration before assigning values. To put it simply, execute the declaration (the variable declared by var or the function declared by function) to allocate all the memory and then assign values to the memory.
2. Loop Optimization
Code:
// Loop 1
For
(Var I = 0; I <myarray. length; I ++ ){
// Use myarray [I] to do something
}
// Cycle 2
Var max;
For
(Var I = 0, max = myarray. length; I <max; I ++ ){
// Use myarray [I] to do something
}
Say:
Loop 2 is faster than loop 1 because you do not need to obtain the length of the array each time. This is often in the form of 1, so you must change it later.
3. hasOwnProperty to cancel the superclass Prototype Method
Code:
// Code 1 for (var I in man) {if (man. hasOwnProperty (I) {// filter the console. log (I, ":", man [I]) ;}}
// Code 2
Var I, hasOwn = Object. prototype. hasOwnProperty; for (I in man) {if (hasOwn. call (man, I) {// filter the console. log (I, ":", man [I]) ;}}
Say:
HasOwnProperty mainly queries whether a method is an object's own method. This allows you to filter out superclass. This can also be used to improve the efficiency of for loop execution.
4. parseInt value conversion
Code:
// Code 1
Var month = "06", year = "09"
Day = "08"; month = parseInt (month, 10); year = parseInt (year, 10 );
Day = parseInt (day); // The result is 0, because it is converted as an 8-digit conversion, so an error occurs.
// Code 2
+ "08" // The result is 8 Number ("08") // 8
Say:
ParseInt actually has two parameters. The following parameters represent the hexadecimal format, so 09 can be converted to 9. code 2 provides other conversion methods. It is worth mentioning that code 2 is faster than Code 1.

5. Do not use var f = function g () {function body ...}
Code:
// Code 1
Var f = function g () {}; typeof g; // "function"
// Code 2
Typeof g; // "function" var f = function g (){};
// Code 3
Var f = function g () {}; f === g; // false f. expando = 'foo'; g. expando; // undefined
// Code 4
Var f = function g () {return 1 ;}; if (false) {f = function g () {return 2 ;};} g (); // 2
Say:
Code 1 and 2 indicate that this object exists no matter where function g is called after function g is declared. It means that declaring a function in this way is irrelevant to not copying the value assignment to f. It can be processed as a function declaration.
Code 3 indicates that f and g are not actually an object. This is actually very confusing and can be considered as a js bug.
Code 4 also shows that the function declaration does not involve actual code. Although you put it in the judgment statement, the Declaration will still exist, however, in actual code writing, we should not use logic to choose how to create a function. Instead, it should be better to place the logic inside the function; otherwise, it may be confusing, although this may be less efficient than the former.
To sum up, do not use var f = function g () {}; as you do not know.

 

From JerryVon's column

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.