Scope of JavaScript variables

Source: Internet
Author: User
Tags getcolor

I. Basic types and reference types

    • A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values.
    • When you assign a value to a variable, the JavaScript parser first determines whether it is a basic type or a reference type, the base data type can manipulate the value stored in the variable directly, and the value of the reference data type is the object that is held in memory, and when the object is manipulated, it actually operates on the object's reference rather than the actual object.

Second, the assignment of variables

    • If you copy the value of a base data type from one variable to another, a new value is created on the variable object, and the value is copied to the location of the new variable, as in the following code:
      var num1 = 5var num2 = NUM1;

    • A reference type, in fact, also copies the value of the original variable into a new variable, except that the copy is actually a pointer to the original variable, and this pointer points to an object stored in the heap. When the copy is complete, the two variables point to the same object in the heap, so changing one of the values will have an effect on the other. The following code:
      var New  var obj2 == "Nicholas"; alert (obj2.name);    //

  

Third, transfer parameters

  • In JavaScript, parameters are passed in accordance with the值类型来传递, even if you pass in a reference type
    function setName (obj) {     = "Nicholas";         } var New Object (); SetName (person); alert (person.name)    ; // "Nicholas"

    The return result of the above code looks like the argument is the pass of the reference type because the start Person object has no properties, and after the SetName method is called, the parameter obj is added with the name parameter. Then the person outside the print person.name unexpectedly is a value, which is very obvious the effect of the reference delivery. But do not be fooled by this phenomenon, JavaScript when passing parameters of the reference type, as long as this parameter does not change, then the reference type to deal with, but only the change effect is completely different.

    function setName (obj) {     = "Nicholas";      New Object ();      = "Greg"varnew  Object (); SetName (person); alert (person.name)    ; // "Nicholas"

    The above example shows the difference, if it is really a reference pass, then obj is re-assigned, and the name parameter is added, and the last person will print the name of Greg, but the result is the original value.

IV. implementation Environment and scope

  • Execution Environment (Execution context) Defines the other data that a function or variable has access to, determines their behavior, and each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object.
  • The global execution environment is the outermost execution environment, in the Web browser, is actually the window object, therefore, all global variables and functions are created as window properties and methods, an execution environment after all the code is executed, the environment is destroyed, The variables and function definitions stored therein are also destroyed (the global execution environment is not destroyed until the application exits and the browser shuts down)
  • Each function has its own execution environment , when the program executes to a function, the function of the environment is created, into the current process of the program, and after the completion of the function, the program process will eject and destroy, return control to the previous execution environment
    • when the code is executed in an environment, the scope chain of the variable object is created, the fundamental meaning of which is to guarantee the orderly access of variables in the execution environment at different levels, as follows:
       var  color = "Blue"  function   ChangeColor () { var  anothercolor =" Red " Span style= "color: #000000;"     >;  function   Swapcolors () { var  tempcolor = Anothercolor;         Anothercolor  = color;     Color  = Tempcolor; //    //  here       You can access anothercolor and color, but you cannot access Tempcolor 
      Swapcolors (); }


      As you can see from the example, the internal environment can access all the external environments on top of it through the scope chain, but the external environment cannot access any variables and functions in the internal environment. These environments are linear and sequential.

    • Each environment can search up the scope chain to query for variables and function names, and any environment cannot enter another execution environment by searching the scope chain down
      var color = "Blue"function  getColor () {         return// "Blue"   

      The color variable inside the GetColor function is obtained from the inside search to the color of the parent's external environment.

    • In the search process, if a local variable with the same name appears, the search stops:
      var color = "Blue"function  getColor () {     var color = "Red";      return  //"Red"

Scope of JavaScript variables

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.