First knowledge of JavaScript

Source: Internet
Author: User

JavaScript is a scripting language that is object-based and event-driven (EventDriven) and has security performance. It is used in conjunction with HTML Hypertext Markup Language, Java scripting language (Java applet) to connect multiple objects in a single Web page, interacting with web customers. The advent of JavaScript enables a real-time, dynamic, interactive relationship between the Web page and the user, allowing the Web page to contain more active elements and more exciting content. At the same time, JavaScript is very short and executed on the client, which greatly improves the browsing speed and interactive ability of the Web page. Follow the small series to continue to pursue the footsteps of BS, today small and small partners to learn about JavaScript knowledge.

Before we start learning JavaScript, let's look at a small example, "Welcome to the world of JavaScript," and the code is as follows:

[HTML]View Plaincopyprint?
  1. <span style="FONT-SIZE:18PX;" ><span style="FONT-SIZE:18PX;" ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
  5. <title> Untitled document </title>
  6. <script language="javascript" type="Text/javascript">
  7. Pop-up message prompt box
  8. Alert ("Merry Drink to the world of JavaScript, and I happy to start the JS Journey" (*∩_∩*)! ");
  9. </Script>
  10. </head>
  11. <body>
  12. </body>
  13. </html>
  14. </span></span>

The results are as follows:

Welcome to the world of JavaScript, and small series of Happy start JS Tour, then, we look at a simple mind map, are JS's entry point of knowledge, such as:

Next, the small part of the important introduction of JS in the common problems, JS commonly used in eight main issues, the following several common questions, detailed introduction .

The first one: case-sensitive;

In fact, this part of the content is mainly to distinguish us in other languages to write, we have used the language: VB is not case-sensitive, and we speak of this JavaScript is case-sensitive. The methods and variables in JS are case-sensitive, so function myfunction{} and Function myFunction () are different. This rule also applies to JavaScript core objects such as: Array and object. When we define an array, the array must start with a capital letter, and when you write lowercase, you will see that the font color of the array changes.


Second: single quotes, double quotes;

Single and double quotes do not have a special difference in JS and can be used to create strings. But as a general rule, most JS developers prefer to use single quotes instead of double quotes, but because the XHTML specification requires that all attribute values be enclosed in double quotation marks. The use of single quotes in JS and the use of double quotation marks on XHTML attributes make it easier and clearer to mix the two codes. Single quotes can contain double quotes, whereas double quotation marks can also contain single quotes.

Third: parentheses ;

In JavaScript, parentheses contain two semantics, one is a delimiter, and the other is an expression. For example: delimiter: (1+3) *3==12 The parentheses are the meaning of the delimiter. Expression: (function () {}) (); a parenthesis in front of it indicates a delimiter, and the next one indicates that the method is executed immediately

Fourth: function invocation and reference;

For example: Var foo=example (); Varfoo1=example; The first represents the return value of the calling function example to Foo, and the second represents assigning a reference to the function to a value foo1.

Fifth one: line change;

Regardless of which quotation marks are used to create a string, the middle of the string cannot contain a forced line break. Line breaks can be preceded by a "\" at the end of the row or by a connector "+" at the front of the next line to tell the browser that the top and bottom lines are contiguous. For example, the following two small examples are equivalent relationships.

[JavaScript]View Plaincopyprint?
    1. <span style="FONT-SIZE:18PX;" >var temp='

    2. +"<ol>"
    3. +' <li class= ' n ' > foo</li> '
    4. +"</ol>"; </span>

And the following one is the same;

[JavaScript]View Plaincopyprint?
    1. <span style="FONT-SIZE:18PX;" >var temp='

    2. "<ol>" \
    3. ' <li class= ' n ' > foo</li> ' \
    4. "</ol>"; </span>

Sixth: semicolon, curly braces optional;
In JavaScript, the end of each line of statements does not have to end with a semicolon, which is optional; the curly braces inside are optional. But in order to avoid some kind of discrepancy, we strongly suggest that we are not all of these things. A few examples: Alert ("Nihao!");  and alert ("Nihao!") There is no semicolon, there is no difference, but the following small example, but completely different oh.

Seventh: overloading is not supported;

Although JavaScript is also object-oriented programming, it is not supported here. For example:
function MyFunction (A, b) {};
function MyFunction (a) ();
Both appear in the script code, and because overloading is not supported, it causes the function below to overwrite the upper function.

Eighth One: scope, closure;
Scope: In fact, in our code programming inside the namespace similar, refers to a property or method has access to the code space, that is, we often say local variables, global variables and so on. For example:

[JavaScript]View Plaincopyprint?
    1. <span style="FONT-SIZE:18PX;" > function myFunction () {
    2. var temp="dGH";
    3. }</span>


The upper temp cannot be accessed outside the function. Closures: A function that is closely related to the scope, which is the ability to read other functions ' internal variables. Can be understood as a function defined inside a function. For example:

[JavaScript]View Plaincopyprint?
    1. <span style="FONT-SIZE:18PX;" >Function f1 () {
    2. n=999;
    3.     function F2 () {
    4. alert (n);
    5. }
    6. return F2;
    7. }
    8.   var result=f1 ();
    9. Result (); </span>

Of course, the use of closures: one is mentioned above can read the variables inside the function, and the other is to keep the values of these variables always in memory. For example:

[JavaScript]View Plaincopyprint?
    1. <span style= "FONT-SIZE:18PX;"   >function f1 () {    
    2. VAR&NBSP;N=999;&NBSP;&NBSP;&NBSP;&NBSP;
    3. Nadd=function () {n+=1}    
    4. function f2 () {    
    5. alert (n); & nbsp;   
    6. }    
    7. return& nbsp;f2;    
    8. }    
    9. var result=f1 ();     
    10. result ();   
    11. Nadd ();     
    12. result ();   </span>  

result is actually the closure F2 function. It runs altogether two times, the first value is 999, the second value is 1000. This proves that the local variable n in the function F1 is kept in memory and is not automatically cleared after the F1 call. These are both the advantages of closures, but also the closure of the defect, the use of closures must be used with caution.

First knowledge of JavaScript

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.