Home >Web Front-end >JS Tutorial >A brief analysis of Javascript variable functions_javascript skills

A brief analysis of Javascript variable functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:02:371010browse
1. Variables
There are two types of values ​​that can be stored in JavaScript variables: original values ​​and reference values.
The original value is stored in a simple field on the stack, that is, the value is stored directly in the location marked by the variable.
The reference value is stored in the object in the heap, and the stack variable stores the pointer value pointing 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 code The code is as follows:

var b = true; // Store on the stack
var num = 20; //Stored on the stack
var b = new Boolen(true); //Stored in the heap
var num = new Number(20); //Stored in the heap

The usual way to generate objects:
new type name var obj = new object()
If there are no parameters, it can also be written as var obj = new object;
You can also use object literals Quantity generation object var obj = {}
2. Function
In JavaScript, functions are objects. They should be treated like other objects in JavaScript. Each function has two implicitly attached Additional parameters this, arguments.
Functions can be: assigned to variables, used as attributes of other objects, used as parameters of other functions, used as return values, and functions can also be created using literals.
Function context:
In object-oriented languages, use the this keyword to refer to the current instance of a class object. The this keyword in JavaScript is different from this in object-oriented languages. In JavaScript, a function is an object, and this refers to the function context in which the current function is called.
You can explicitly specify function context through the call() and apply() methods. The first parameter of Call is used as the context of the calling function, and the other parameters are passed into the called function as parameters of the called function. Apply() is similar to Call(), except that the second parameter is an array.
Copy code The code is as follows:

var obj = {
m:"hello"
}
var m="hi";
var say=function()
{
alert(this.m); //this points to the function calling context
}
say();//hi, window is the calling context
say.call(obj);// hello, obj is the function calling context at this time
say.call(window);// hi, window is Calling context

Scope:
Parameters and variables defined in a function are not visible outside the function, and variables defined anywhere in a function are not visible anywhere in the function. Everywhere is visible.
Copy code The 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
The so-called closure means that the function can use variables defined outside the function, and the function can access the variables when it is created. context.
Copy code The 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

Pay attention to 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 to an object returned by the function that contains two methods, and these two methods enjoy the privilege of accessing the value variable.
Let’s take another example of an internal function accessing local variables of an external function that is widely circulated on the Internet to illustrate closure. Click the corresponding list item to pop up the corresponding sequence number.
Copy code The code is as follows:

  • test1

  • test2

  • test3

  • 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 in the constructor, so 3
    }
    }
    alert(i); //3
    }
    test();
    The following writing method can get the correct result:

    Copy the code The 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); //Every time the constructor Immediately pass the i value in for execution. Now the function bound to onclick is the function returned after executing the
    //line
    }
    alert(i); //3
    }
    test( );

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn