About JS variable and scope detailed _javascript skill

Source: Internet
Author: User

ECMAScript variable:

1. Basic type value (simple data segment)

2. Reference type value (an object that may be composed of past values) → objects saved in memory

------

Dynamic properties: You can only dynamically add new attributes to a reference value for future use.

------

To copy a variable value:

Copy of base type value → create a new value on variable object → copy to new variable (no effect)

Reference type value replication → Copy the value stored in the variable object into the new variable allocation space (copying a pointer, pointing to the same object, interacting with each other)

------

Pass parameters:

Parameters for all functions in ECMAScript are passed by value.

(Values outside the function are copied to parameters inside the function.)

→ The parameters of the ECMA function can be imagined as local variables. )

Explanation of Reason:

Saying one:

The function parameter is a kind of local variable, the external value passes to the function parameter, the internal external is not mutually shadow, the reference passes the copy is the address, therefore his interior exterior has the influence mutually.

Argument two: Object angle (parameter is an object obj)

------

Objects are passed by value → parameter objects and external objects reference the same object → The object pointed to by the external object has only one in the heap memory and is a global variable.

Do not mistake the local variables that are reflected in the global scope (external) of the modified object in the local effect → function is destroyed immediately after the function is executed.

------

Execution environment (sometimes referred to as "environment") and scope

The execution environment defines a white myrtle or function that has access to other data that determines their respective behavior → each execution environment has a variable object associated with it → all variables and functions defined in the environment are stored in this object.

------

Each function has its own execution environment → when the execution flow enters a function → The function environment is pushed into an environment stack → it pops up after function execution and returns control to the previous execution environment

When code is executing in an environment, a scope chain of variable objects is created (scope chain)

Scope Chain Purpose: Ensure orderly access to all variables and functions that the execution environment has access to.

The front end of the scope chain is always the variable object in the environment where the currently executing code resides
------

The internal environment can be scoped to all external environments, but the external environment does not have access to the internal environment (the environment is linear, orderly → search the scope chain up to query variables and function names)

Function parameters are also treated as variables

------

Extend scope chain:

Some statements can temporarily add a variable to the front-end chain of the scope (the variable is removed after the code executes)

Catch block for 1.try-catch statement

2.with statement
------

Javas no block-level scopes

Here's a simple way to distinguish between block-level scopes and function scopes

The above about JS variables and scope of the explanation is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.