1. Variables
There are two types of values that can be stored in javascript variables: original value and reference value.
The original value is stored in a simple field on the stack, that is, the value is stored directly in the position marked by the variable.
The referenced object stored in the heap, and the variables in the stack hold the pointer value to the object in the heap.
There are 5 basic types in javascript: Undefined, Null, Boolean, Number, String.
Reference types are actually objects, similar to the concept of class instances in other languages.
var b = true; // Store on stack
var num = 20; //Stored on stack
var b = new Boolen(true); //Stored in the heap
var num = new Number(20); // Store in the heap
The usual way to generate objects:
new + type name var obj = new object()
If there are no parameters, you can also write as var obj = new object;
You can also use object literals to generate objects var obj = {}
2. Function
In javascript, functions are objects, and functions should be treated like other objects in javascript. Each function implicitly appends two extra parameters this, arguments.
Functions can be assigned to variables, as attributes of other objects, as parameters of other functions, as return values, and also using literals to create functions.
Function context:
Use this keyword to refer to the current instance of a class object in an object-oriented language. The keyword this in javascript is different from this in object-oriented language. In javascript, a function is an object, and this refers to the function context that calls the current function.
The function context can be explicitly specified through the call() and apply() methods. The first parameter of the Call is used as the context of the calling function, and other parameters are passed into the called function as parameters of the called function. Apply() and Call() are similar, except that the second parameter is an array.
var obj = {
m:"hello"
}
var m="hi";
var say=function()
{
alert(); //this points to the function call context
}
says();//hi, window is the calling context
(obj);// hello, at this time obj is the function call context
(window);// hi, window is the calling context
Scope:
Parameters and variables defined in a function are not visible outside the function, and variables defined anywhere in a function are visible anywhere in the function.
var obj = function() {
var num = 1;
return { getValue: function() {
alert(num); //undefined
var num = 2;
alert(num); //2
}
}
} ();
();
Closure
The so-called closure is that a function can use variables defined outside the function, and the function can access the context environment at creation.
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;
}
}
} ();
('a');
alert(()); //1
Note that the last line (),() is the call operator, which means that the function is called immediately and returns the call result. So obj does not refer to a function, but refers to an object that contains two methods returned by the function, and these two methods have the privilege of accessing the value variable.
Let’s give an example of internal function access external function local variables that are widely circulated on the Internet to illustrate the closure. Click the corresponding list item to pop up the corresponding order number.
<li >test1</li>
<li >test2</li>
<li >test3</li>
var test = function() {
var num = ("li");
var i;
for (i = 0; i < ; i++) {
num[i].onclick = function() {
alert(i); //Inner function can access external function variables, the final value of i is 3,
// Instead of the i value when constructor, so 3 pops up here
}
}
alert(i); //3
}
test();
The following writing method can get the correct results:
var test = function() {
var num = ("li");
var i;
for (i = 0; i < ; i++) {
num[i].onclick = function(i) {
return function() {
alert(i + 1);
}
} (i); //Every time the constructor is constructed, the i value is immediately passed to execute. Now the function bound to onclick is execution
//The function returned after the line
}
alert(i); //3
}
test();
There are two types of values that can be stored in javascript variables: original value and reference value.
The original value is stored in a simple field on the stack, that is, the value is stored directly in the position marked by the variable.
The referenced object stored in the heap, and the variables in the stack hold the pointer value to the object in the heap.
There are 5 basic types in javascript: Undefined, Null, Boolean, Number, String.
Reference types are actually objects, similar to the concept of class instances in other languages.
Copy the codeThe code is as follows:
var b = true; // Store on stack
var num = 20; //Stored on stack
var b = new Boolen(true); //Stored in the heap
var num = new Number(20); // Store in the heap
The usual way to generate objects:
new + type name var obj = new object()
If there are no parameters, you can also write as var obj = new object;
You can also use object literals to generate objects var obj = {}
2. Function
In javascript, functions are objects, and functions should be treated like other objects in javascript. Each function implicitly appends two extra parameters this, arguments.
Functions can be assigned to variables, as attributes of other objects, as parameters of other functions, as return values, and also using literals to create functions.
Function context:
Use this keyword to refer to the current instance of a class object in an object-oriented language. The keyword this in javascript is different from this in object-oriented language. In javascript, a function is an object, and this refers to the function context that calls the current function.
The function context can be explicitly specified through the call() and apply() methods. The first parameter of the Call is used as the context of the calling function, and other parameters are passed into the called function as parameters of the called function. Apply() and Call() are similar, except that the second parameter is an array.
Copy the codeThe code is as follows:
var obj = {
m:"hello"
}
var m="hi";
var say=function()
{
alert(); //this points to the function call context
}
says();//hi, window is the calling context
(obj);// hello, at this time obj is the function call context
(window);// hi, window is the calling context
Scope:
Parameters and variables defined in a function are not visible outside the function, and variables defined anywhere in a function are visible anywhere in the function.
Copy the codeThe code is as follows:
var obj = function() {
var num = 1;
return { getValue: function() {
alert(num); //undefined
var num = 2;
alert(num); //2
}
}
} ();
();
Closure
The so-called closure is that a function can use variables defined outside the function, and the function can access the context environment at creation.
Copy the 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;
}
}
} ();
('a');
alert(()); //1
Note that the last line (),() is the call operator, which means that the function is called immediately and returns the call result. So obj does not refer to a function, but refers to an object that contains two methods returned by the function, and these two methods have the privilege of accessing the value variable.
Let’s give an example of internal function access external function local variables that are widely circulated on the Internet to illustrate the closure. Click the corresponding list item to pop up the corresponding order number.
Copy the codeThe code is as follows:
<li >test1</li>
<li >test2</li>
<li >test3</li>
var test = function() {
var num = ("li");
var i;
for (i = 0; i < ; i++) {
num[i].onclick = function() {
alert(i); //Inner function can access external function variables, the final value of i is 3,
// Instead of the i value when constructor, so 3 pops up here
}
}
alert(i); //3
}
test();
The following writing method can get the correct results:
Copy the codeThe code is as follows:
var test = function() {
var num = ("li");
var i;
for (i = 0; i < ; i++) {
num[i].onclick = function(i) {
return function() {
alert(i + 1);
}
} (i); //Every time the constructor is constructed, the i value is immediately passed to execute. Now the function bound to onclick is execution
//The function returned after the line
}
alert(i); //3
}
test();