Analysis of JavaScript variable replication, parameter transfer and scope chain, analysis of javascript

Source: Internet
Author: User

Analysis of JavaScript variable replication, parameter transfer and scope chain, analysis of javascript

Today, while reading a book, I found a vague knowledge of Javascript: the scope chain of JS, therefore, I learned the content related to the scope chain by reading materials. Today's study notes mainly include the following keywords: Variable, parameter transfer, execution environment, variable object, and scope chain.

1. Variables

Note the following two points for variables: Variable declaration and copy variable value.

Variable declaration is certainly familiar to everyone. In JS, we declare variables through the var keyword. JS stipulates that variables declared through var will be added to the nearest Environment. If the Declaration and initialization of a variable does not use the var keyword, this variable will be added to the global environment.

The process of copying variable values varies depending on the type of variables. If a variable is of the basic type, a new space is allocated to the newly copied variable when the variable value is copied. The two variables do not affect each other. If the variable is of the reference type, in fact, the replication operation directs two variables to the same memory space and modifies one of them, and the other changes accordingly. The legends in Javascript advanced programming are actually very vivid.

2. parameter transfer

Parameters in JavaScript are passed by value. There is no confusion when the basic type is used as a parameter. If the reference type is used as a parameter, it is similar to the following example:

Function setName (obj) {obj. name = "tom";} var person = new Object (); setName (person); alert (person. name); // display tom

In this example, we modified the variable content in setName, which also takes effect outside the function. At the beginning, I thought that undefined or error should be displayed during program execution, but the modified value in the function scope is displayed. After analyzing the entire process of passing parameters, this problem is solved. There is an important step in the process of passing parameters: copying variable values. When we call a function, we actually perform the obj = person step, so according to the above mentioned characteristics of copying the reference type variable value, when we modify the obj, at the same time, the person value is modified. Therefore, the JS parameter is passed by value, and it can only be passed by value.

3. Execution Environment, variable object, and scope chain

My understanding of the execution environment and execution environment is similar to that of classes and objects:

The execution environment defines other data that can be accessed by variables, functions, and functions. When the execution environment is activated, A variable object is created based on the execution environment and provided to the parser. The execution environment is like a class, and the variable object corresponds to an object.

When an execution environment is activated, it is pushed to the top of a stack for execution. After the execution is completed, it is removed from the stack and executed before it enters the stack environment, and so on.

The scope chain is equivalent to a stack that stores variable objects. The earlier the variable objects created in the activated execution environment are located below, the more variable objects in the active execution environment are located at the top of the stack. If the current execution environment is complete, you need to remove the variable objects (corresponding to the execution environment) at the top of the stack from the top of the stack.

When the execution environment is running, the parser needs to access the variables and other data from the top of the scope to find, that is, from the variable object corresponding to the current execution environment, if not, go down to the variable object corresponding to the outer execution environment and continue until you find the desired object or the variable object in the global environment. Therefore, this search method shows that too many variables defined in the global environment affect program performance.

What I learned today is mainly conceptual and abstract. However, this part is the basis for all the subsequent knowledge, such as the closure, inheritance, and prototype. You must have a good understanding of this part of content before you can learn more clearly, therefore, this part of content should be learned over and over again to believe in the warmth of the new, the Ancients do not bully me (too) again.

Articles you may be interested in:
  • Encode and decode JavaScript Base64 to transmit URL parameters.
  • Analysis of actual application code for passing JS Parameters
  • In-depth analysis of js function execution environment and scope chain
  • Javascript nested functions (scope chain)
  • Js (detailed analysis of transmission of parameters of basic data types and reference types)
  • Scope chains and closures in JavaScript
  • In-depth usage of Javascript Functions, recursion, and closures (execution environment, variable object, and scope chain)
  • Js setTimeout parameter transfer
  • Example of using Javascript to obtain the parameter transfer value of HTML static pages
  • A deep understanding of the lexical scopes and scopes of JavaScript
  • Example of passing JS Parameters
  • Example of parameter passing in javascript Functions
  • Four parameter transmission formats: URL, hyperlink, js, and form
  • JavaScript function calls and parameter passing

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.