Learning jQuery from scratch the JavaScript you must know

Source: Internet
Author: User

I recently interviewed some people and found that even experienced developers often blur some basic theories and details. I wrote this article because I learned the following content for the first time and found that I had some gains and insights. in fact, we can easily ignore more details about javascript. This article is only the tip of the iceberg. I hope everyone can gain some benefits through this article.

I. Javascript object-oriented

Javascript is an object-oriented language. Although many books have explained it, many novice developers still do not understand it.

Create object

Ps: I have previously written a detailed article on Object creation (prototype method, factory method, etc.) but cannot find it. If I can find it again, I will add it. the following is a brief introduction.

In C #, we use the new keyword to create an object. In javascript, we can also use the new Keyword:

 
 
  1. var objectA = new Object(); 

But in fact, "new" can be omitted:

 
 
  1. var objectA = Object(); 

However, we recommend that you keep the syntax unchanged,An object is always declared with the new keyword.

Create attributes and assign values

In javascript, attributes do not need to be declared. They are automatically created when values are assigned:

 
 
  1. objectA.name = "my name"; 

Access attributes

Generally, we use "." To access object properties hierarchically:

 
 
  1. alert(objectA.name); 

Nested attributes

The attributes of an object can also be any javascript Object:

 
 
  1. VarObjectB = objectA;
  2. ObjectB. other = objectA;
  3. // At this time, the three values below are equivalent, and changing any of the values changes the other two values. 
  4. ObjectA. name;
  5. ObjectB. name;
  6. ObjectB. other. name;

Use Index

If there is an attribute named "school. college ", so we cannot pass ". "Access, because" objectA. school. the college statement is the college attribute of the school attribute object of objectA.

In this case, you need to set and access attributes through the index:

 
 
  1. objectA["school.college"] = "BITI";  
  2. alert(objectA["school.college"]); 

The following statements are equivalent:

 
 
  1. objectA["school.college"] = "BITI";  
  2.  var key = "school.college" 
  3. alert(objectA["school.college"]);  
  4. alert(objectA["school" + "." + "college"]);          
  5. alert(objectA[key]); 

JSON format syntax

JSON refers to Javascript Object Notation, that is, Javascript Object Notation.

We can use the following statement to declare an object and create attributes at the same time:

 
 
  1. // JSON 
  2. VarObjectA = {
  3. Name:"MyName",
  4. Age: 19,
  5. School:
  6. {
  7. College:"University",
  8. "High school":"High School"
  9. },
  10. Like :["Sleeping","C #","Sleeping"]
  11. }

The JSON syntax format uses "{" and "}" to represent an object. The format of "attribute name: value" is used to create an attribute. Multiple Attributes are separated.

In the preceding example, the school attribute is an object. The like attribute is an array. After an object is created using a JSON string, you can access the attribute in the form of "." or an index:

 
 
  1. objectA.school["high school"];  
  2. objectA.like[1]; 

Static Method and instance method

Static Method is a method that can be used without declaring an instance of the class.

An instance method is a method that requires you to declare an instance of a class using the "new" keyword before you can access the instance.

 
 
  1. FunctionStaticClass (){};// Declare a class 
  2. StaticClass. staticMethod =Function() {Alert ("Static method")};// Create a static method 
  3. StaticClass. prototype. instanceMethod =Function(){"Instance method"};// Create an instance method 

The staticClass class is declared first, and a static method staticMethod and a dynamic method instanceMethod are added to it. The difference is that prototype attributes are used to add dynamic methods.

You can directly call static methods:

 
 
  1. staticClass.staticMethod(); 

However, dynamic methods cannot be called directly:

 
 
  1. StaticClass. instanceMethod ();// The statement is incorrect and cannot be run. 

You must instantiate it before calling it:

 
 
  1. VarInstance =NewStaticClass ();// First instantiate 
  2. Instance. instanceMethod ();// The instance method can be called on the instance 

2. The Global object is the window attribute.

Normally

     
    

    However, we should also know that the global variable objectA is actually created on the window object and can be accessed through the window object:

     
     
    1. window.objectA 

    Iii. What is a function?

    We all know how to create a global function and call it:

     
     
    1. function myMethod()  
    2. {  
    3. alert("Hello!");  
    4. }   
    5. myMethod();  

    In fact, like a global object, the name of the method created using the function keyword (you can also create a class) is actually the myMethod attribute created for the window object, and the value is an anonymous method, the preceding statement is equivalent:

     
     
    1. window.myMethod = function()  
    2. {  
    3. alert("Hello!");  

    No matter which method is used for the Declaration, the function name is used to create the properties of the window object during actual saving, and the value is only the function body without the function name.

    Therefore, the following three declaration methods are equivalent:

     
     
    1. function myMethod()  
    2. {  
    3. alert("Hello!");  
    4. }  
    5. window.myMethod = function()  
    6. {  
    7. alert("Hello!");  
    8. }  
    9. myMethod = function()  
    10. {  
    11. alert("Hello!");  

    4. What is "this "?

    In C #, this variable usually refers to the current instance of the class. in javascript, the "this" in javascript is the context of the function, not the declaration, but the call. because the global function is actually a window attribute, this indicates the window object when the global function is called on the top layer.

    The following example demonstrates all of this:

     
     
    1. var o1 = { name: "o1 name" };  
    2.         window.name = "window name";  
    3.  
    4.         function showName()  
    5.         {  
    6.             alert(this.name);  
    7.         }          
    8.           
    9.         o1.show = showName;  
    10.         window.show = showName;  
    11.  
    12.         showName();  
    13.         o1.show();  
    14.         window.show(); 

    Result:

    The result shows that this points to the window object when calling the function at the top level and calling the function using the window object, and this points to the current object when calling the function in the object.

    5. Closure in javascript

    The concept of closure is hard to understand. Let's first look at the definition of closure:

    A closure is an expression (usually a function) that has many variables and is bound to these variables. Therefore, these variables are part of the expression.

    Simple expression:

    Closure refers to the variables from the environment when the function instance is executed.

    Let's take a look at the following example:

     
     
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. "http://www.w3.org/1999/xhtml">  
    3.     <title></title>  
    4. <body>  
    5.     <div id="divResult"></div>  
    6.     <script type="text/javascript">  
    7.         function start()  
    8.         {  
    9.             var count = 0;  
    10.             window.setInterval(function()  
    11.             {  
    12.                 document.getElementById("divResult").innerHTML += count + "<br/>";  
    13.                 count++;  
    14.             }, 3000);  
    15.         };  
    16.         start();  
    17.     </script>  
    18. </body>  
     
     

    Count is a variable in the start function. Generally, we understand the role of count. Therefore, in the start () function, it should disappear after the start () function is called. however, the result of this example is that the count variable will always exist, and 1 is added each time:

    Because the count variable is part of the closure of the anonymous function created in setInterval (that is, the function containing count ++!

    In other words, the closure is the function itself first. For example, the above anonymous function itself is added with the count variable required for running the function.

    Closures in javascript are implicitly created, but they do not need to be explicitly created as other languages that support closures. we rarely encounter this in C # Because C # cannot declare a method again in the method. while calling another method in one method usually uses parameters to pass data.

    1. JQuery-like Flash-beating buttons
    2. AJAX/PHP/JQuery/CSS design drag-and-pull shopping cart
    3. Sharing 24 useful jQuery plug-ins
    4. Seven amazing jQuery Learning Websites
    5. JQuery drag layout for Database Synchronization of sorting results

    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.