Javascript Variable Functions

Source: Internet
Author: User

I. Variables
Javascript variables can store two types of values: the original value and the reference value.
Raw values are stored in simple fields on the stack, that is, the values are directly stored in the positions indicated by the variables.
The reference value is stored in the heap object. The variable in the stack stores the pointer value pointing to the object in the heap.
There are five basic types in javascript: Undefined, Null, Boolean, Number, String.
The reference type is actually an object, similar to the concept of class instances in other languages.
Copy codeThe Code is as follows:
Var B = true; // stored on the stack
Var num = 20; // stored on the stack
Var B = new Boolen (true); // store it in the heap
Var num = new Number (20); // stored in the heap

Generally, the object generation method is as follows:
New + type name var obj = new object ()
If there are no parameters, you can also write var obj = new object;
You can also use the object literal to generate the object var obj = {}
Ii. Functions
In javascript, functions are objects. They should be treated like other objects in javascript. Each function is implicitly attached with two additional parameters: this and arguments.
Functions can be assigned to variables as attributes of other objects, as parameters of other functions, as return values, and can be used to create functions.
Function context:
Use the this keyword in the object-oriented language to reference the current instance of the Class Object. This keyword in javascript is different from this in object-oriented languages. In javascript, a function is an object, and this references the context of the function that calls the current function.
You can explicitly specify the function context using the call () and apply () methods. The first parameter of Call is used as the context for calling the function, and other parameters are used as parameters of the called function to pass in the called function. Apply () is similar to Call (), but the second parameter is an array.
Copy codeThe Code is as follows:
Var obj = {
M: "hello"
}
Var m = "hi ";
Var say = function ()
{
Alert (this. m); // this indicates the context of the function call.
}
Say (); // hi, window is the call context
Say. call (obj); // hello. In this case, obj is the context of the function call.
Say. call (window); // hi, window is the call context

Scope:
Parameters and variables defined in a function are invisible outside the function, and variables defined at any position in a function are visible anywhere in the function.
Copy codeThe Code is as follows:
Var obj = function (){
Var num = 1;
Return {getValue: function (){
Alert (num); // undefined
Var num = 2;
Alert (num); // 2
}
}
}();
Obj. getValue ();

Closure
A closure is a variable that can be defined outside a function. A function can access the context when it is created.
Copy codeThe Code is as follows:
Var hello = "hello word! ";
Function sayHello (){
Alert (hello );
}
SayHello ();
Var obj = function (){
Var value = 0;
Return {
SetValue: function (val ){
Value + = typeof val = 'number '? Val: 1;
},
GetValue: function (){
Return value;
}
}
}();
Obj. setValue ('A ');
Alert (obj. getValue (); // 1

Note that () in the last line is the call operator, which indicates that the function is called immediately and the call result is returned. So obj does not reference a function, but refers to an object returned by the function that contains two methods, and these two methods are privileged to access the value variable.
Here is an example of a wide spread of internal functions accessing local variables of external functions on the Internet to illustrate closures. Click the corresponding list item to display the corresponding sequence number.
Copy codeThe Code is as follows:
<Li id = "a"> test1 </li>
<Li id = "B"> test2 </li>
<Li id = "c"> test3 </li>
Var test = function (){
Var num = document. getElementsByTagName ("li ");
Var I;
For (I = 0; I <num. length; I ++ ){
Num [I]. onclick = function (){
Alert (I); // internal functions can access external function variables. The final value of I is 3,
// Instead of the I value of the constructor, SO 3 is displayed here.
}
}
Alert (I); // 3
}
Test ();

The following code returns the correct result:
Copy codeThe Code is as follows:
Var test = function (){
Var num = document. getElementsByTagName ("li ");
Var I;
For (I = 0; I <num. length; I ++ ){
Num [I]. onclick = function (I ){
Return function (){
Alert (I + 1 );
}
} (I); // The I value is immediately passed into and executed every time the constructor is constructed. Now the onclick function is
// Function returned after the row
}
Alert (I); // 3
}
Test ();

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.