JavaScript variable with the same name

Source: Internet
Author: User

I write a process:
Before the process, you must write down what the identifier is. A word is the property of variable object. This object is determined by the different execution environments. For example, the global environment of variable object is global, the function code type of execution environment, it is acitvation object, and eval code is in the eval code calling con The text of the variable object. Of course the Eval code is a little bit of a statement. Reference: ECMA262 Edition5 the direct call section of the so-called eval code. In summary, all identifiers are intrinsic objects variable object properties.

The variable initialization process is this:
After the lexical scan. To find out all the identifiers,
3 identifiers: Function name, formal parameter name, variable name, are initialized in three phases:

The first step:
Initializes the name of the function, which is a very special process. Because you need to create a function object at the same time, and then add the function name, as the property name, to variable object, if there is already an existing name, discard the process of adding the property name, and directly refer to the created function object as the value of this property. If an attribute with the same name was not previously present. Add the current function name as the property name to the variable object. and set the attribute set for this property: The Set property cannot be deleted by the delete operator. Then the property is assigned a function object reference.

Step Two:
Parameter initialization, again, encounters the property name of the variable object with the same name, discarding the Add attribute operation. If the property name is available, add the attribute and assign the undefined to this property. and set the attribute set so that the property cannot be deleted.

Step Three:
Variable initialization, as with formal parameter initialization.

Here is an exception to the initialization process, which occurs in eval code.
The variable initialization in eval code does not set the [[Dontdelete]] attribute when adding a property to calling context's variable object. So it can be deleted by delete.

This is why, on the surface, the function name takes precedence over the formal parameter, and the parameter is higher than the variable name.

The nature of the second example is the scope chain, and FN as the function name does not exist in the inner scope. So this test case is inappropriate and should be tested for the same scope. Or use the same variable object.

There seems to be a problem with several other test cases. That is, there is no distinction between two processes, variable initialization process, earlier than the execution period.

So a = 1; When the statement executes, the initialization is already over. Nature does not reflect priority. Must be the last assignment statement to be executed. The result will eventually be affected. Unless this thing has readonly characteristics.

JavaScript variable with the same name

Related Article

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.