variables, scopes, and memory issues (III)

Source: Internet
Author: User
Tags getcolor

Early in the morning, I wish to have a good mood today, laugh. This should be the last one in this chapter.

From the previous two of the introduction, you can also see that the variables in JS are generally divided into two, one is the basic type is a reference type, in practice, most of the test or need to depend on the type of variable to determine the next operation, Usually use the two methods of TypeOf and Instanceof, although both methods can verify the data type of the variable, but there are some differences.

typeof Method:

var Temp;console.log (typeof temp); // console output undefinedtemp=Object (); Console.log (typeof temp); // Console Output Objecttemp=function() {};console.log (typeof temp); // Console output functionetc ...

As you can see in the example above, typeof will directly get the type of the variable, but it is always a large class, for example, for the three kinds of obejct,date,regexp, the answer is the same:

temp=Object (); Console.log (typeof temp); // Console Output Objecttemp=new  Date (); Console.log (typeof temp); // Console Output Objecttemp=new  RegExp (); Console.log (typeof temp); // Console Output Object

Instanceof Method:

Unlike the above, the Instanceof method does not directly tell the type of fatigue, but instead takes a way of inquiring to indirectly get the type of the element, and it can identify the three types of object,date,regexp, and not give them a general designation like TypeOf.

var temp=instanceof Object); // truetemp=newinstanceof Array); // truetemp=newinstanceof RegExp); // true

Let's talk about the execution environment and scope in JS

An important concept in JS is the execution environment, each execution environment has a variable object associated with it, the outermost execution environment is the global execution environment, in the Web browser, the global execution environment is considered a Window object, and in the global execution environment, each function has its own execution environment, When the execution flow enters a function, the environment of the function is pushed into an environment stack, and after the function executes, the stack will eject the environment and return control to the previous execution environment. Due to the existence of such an execution environment, these execution environments can be organized in a chain, that is, when the contemporary code executes in an environment, a scope chain of variable objects is created, and the variables called in the function are looked up in the order of the chain, and if you backtrack to the head, That is, the variable is not found in the global environment, it will be an error.

varColor= "Red"; Console.log (GetColor ());//Console output RedConsole.log (SetColor ());//Console Output GreenfunctionGetColor () {console.log (anothercolor);//error, Anothercolor is not defined    returnColor//look up the variable color, which is the global variable}functionSetColor () {varColor= "Green";//here, the color belongs to the local variable.    returncolor;}

In the above example, the function getcolor involves two execution environments, getcolor the local execution environment and the global execution environment, there is no definition of color in the local execution environment, so when the GetColor function is called, it is looked up along the scope chain, It is found that there is a definition of color in the global execution environment, so the variable color in the function getcolor is the variable color in the global execution environment, but the same anothercolor used in GetColor is less fortunate. According to Anothercolor's chain of action to find out, found in the execution environment of this function does not define the variable, continue upward, in the global execution environment also did not find the definition of this variable, this time will be an error.

The same method of analysis SetColor function also involves two execution environment, when using the color variable to look up along the chain of action, all of a sudden in the function execution environment to find the declaration of this variable, this time will not continue to look up, So the color variable returned in SetColor is consistent with the variable color defined inside the function, not affected by the color defined in the global environment.

Access to variables in the order of the chain of action, always following the internal environment, can access all external environments through the scope chain, but the external environment cannot access any variables and functions in the internal environment.

var color= "Blue"; function ChangeColor () {    var anothercolor= "Red";     function swapcolors () {        var tempcolor=Anothercolor;        Anothercolor=color;        Color=tempcolor;    }    Swapcolors ();} ChangeColor ();

Here, the function swapcolors involves three scopes, namely the swapcolors local scope, the ChangeColor parent scope and the global parent scope, the function ChangeColor involves two scopes, are ChangeColor local scope and global parent scope, respectively. Call Swapcolors can access the three variables of Tempcolor,anothercolor,color, call ChangeColor can only access to anothercolor,color these two variables, Only the color variable can be accessed under the global scope.

I feel like I'm going to wrap up a wave tomorrow and laugh.

variables, scopes, and memory issues (III)

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.