Thinking on the reference of JavaScript function

Source: Internet
Author: User

This afternoon, thinking about calling the function, what about the arguments passed to the executing function. When the type of the value is different, there is no change.

When the value of the parameter is the basic type, this time is passed by value, this is definitely clear, but the time is the reference type, this time what is the situation ah.

  

  

When we pass an object as an argument, we find out how to run the Funx function, but the global object A variable is also modified, which is a bit confusing. When a parameter is passed, it is not passed by value, why is the property of the object in the external environment modified. It seems that this is a reference pass. However, if we run a function like the following:

found that the output of the results did not become 20, in fact, when the function parameters become reference types, we have changed the value, we know that the basic data type JS is stored in the stack memory, and the reference type is stored in the heap memory. When we point to this reference type with a variable, the variable is not determined to point to the reference type, but rather a pointer to the memory in which the reference type resides. When we pass a reference type in a function, it's actually the same thing, we just pass the pointer in.

Thinking on the reference of JavaScript function

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.