Javascript BASICS (II) -- Considerations

Source: Internet
Author: User

I originally wanted to write a blog, but I felt that it was too long and I divided it into two. In the previous article, we will summarize some precautions for beginners of javascript:

 

Ii. Notes:

Note the case sensitivity and single quotation marks at. As mentioned in the previous blog in double quotation marks! Let's start with the third one.


3. brackets: In JavaScript, brackets contain two types of semantics: delimiter and expression.

For example, the separator: (1 + 3) * 3 = 12.

Expression: (function () {}) (); here, a bracket represents the separator, And the other represents the immediate execution of this method.


4. function call and reference:

For example: VaR
Foo = example ();

Varfoo1 = example;

The first one indicates that the return value of the function example is returned to Foo, and the second one indicates that the reference of the function is assigned to a value foo1.


5. line feed:

No matter which quotation marks are used to create a string, the string cannot contain a forced line break.

To wrap a line, you can add "\" at the end of the line or use the connector "+" at the front of the next line to tell the browser that the upper and lower lines are one line.
Continuous.

For example:

        var temp='

It is equivalent:

        var temp='

6. semicolon (;). Optional braces:

In JavaScript, the end of each line of statements does not have to end with a semicolon, which is optional; the braces inside are also optional. However, to avoid some differences, we strongly recommend that you do not complete these things. Examples:

Alert ("nihao! "); And alert (" nihao
! ") There are no semicolons, there is no difference;


But they are completely different!


7. overload is not supported:

Although Javascript is also object-oriented, It is not supported here.

For example:

Function myfunction (a, B ){};

Function myfunction ()();

Both of them appear in the script code at the same time. Because overload is not supported, the following functions will overwrite the above functions.



8. Scope and closure:

Scope: in fact, it is similar to the namespace in code programming. It refers to the code space where an attribute or method has access permissions, that is, the local variables and global variables that we often call. For example:


Function myfunction (){

VaR temp = "L133 ";

}


The above temp cannot be accessed outside the function.

Closure: it is closely related to the scope. It is a function that can read internal variables of other functions. It can be understood as a function defined inside a function.

For example:

function f1(){    n=999;    function f2(){      alert(n);    }    return f2;  }  var result=f1();  result(); // 999

Of course, the purpose of closure: one is to read the internal variables of the function mentioned above, and the other is to keep the values of these variables in the memory. For example:


 function f1(){    var n=999;    nAdd=function(){n+=1}    function f2(){      alert(n);    }    return f2;  }  var result=f1();  result(); // 999  nAdd();  result(); // 1000

Result is actually the F2 function of the closure. It runs twice in total. The first value is 999, and the second value is 1000. This proves that the local variable N in function F1 has been stored in the memory and is not automatically cleared after F1 is called.


These are both the advantages and defects of the closure, and must be used with caution when using the closure.

 

To sum up, we need to pay attention to some things of JavaScript learning. Of course there are many other things, which are a few simple points here. More learning requires constant practice.


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.