Uncle Tom in-depth understanding of JavaScript learning Notes

Source: Internet
Author: User
Tags hasownproperty

// Global Variables function func () {    //  "undefined"    var myname = "local";     // "Local" }func ();

Code processing is divided into two phases, the first of which is the variable, function declaration, and the normal format of the parameter creation, which is a phase of parsing and entering the context. The second stage is code execution, function expressions and unqualified identifiers (for declared variables) are created

for (var i = 0; i < myarray.length; i++) {   //use myarray[i] to do something}

The trouble with collections is that they query basic documents (HTML pages) in real time. This means that every time you access the length of any collection, you want to query the DOM in real time, and DOM operations are generally more expensive.

 for (var i = 0, max = myarray.length; i < Max; i++) {   //  use myarray[i] do Something}

So, in this loop, you only retrieve the length value once.

for-inLoops should be used on the traversal of non-array objects, and the use for-in of loops is also known as "enumerations". hasOwnProperty()method, you can filter out attributes from the prototype chain when traversing object properties.

//ObjectvarMan ={hands:2, Legs:2, heads:1};//Somewhere in the code.//One method was added to all objectsif(typeofObject.prototype.clone = = = "undefined") {Object.prototype.clone=function () {};}//for-in Cycle for(varIinchMans) {   if(Man.hasownproperty (i)) {//FilterConsole.log (i, ":", Man[i]); }}/*Console Display Results hands:2legs:2heads:1*/

To avoid the implicit type conversions that cause confusion, always use the = = = and!== operators when you compare values and expression types.

var zero = 0; if false {   //  do not execute because zero is 0 instead of false}//  Reverse Example if false ) {   //  executed ...}

Eval () is the devil

// Reverse Example var property = "Name",alert (eval ("obj." + property ); // better. var property = "Name"; alert (Obj[property]);

A function declaration can only appear in a program or function body. In the syntax, they cannot appear in blocks (block) ({...} ), for example, cannot appear in an if, while, or for statement. Because block (block) can contain only statement statements, it cannot contain source elements such as function declarations. On the other hand, a closer look at the rules will also reveal that the only thing that can make an expression appear in block is to make it part of an expression statement. However, the specification explicitly specifies that an expression statement cannot begin with a keyword function. This, in effect, means that the function expression also cannot appear in the statement statement or block (because block is made up of statement statements).

Uncle Tom in-depth understanding of JavaScript learning Notes

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.