Pass-by-value delivery of function parameters in JavaScript and pass-by-reference (that is, pass by address)

Source: Internet
Author: User

First of all, theparameters of all functions in JavaScript are passed by value! There is no pass by reference!

in speaking of passing Parameters let's start with pointers.

Learn the C pointer should know that the pointer variable is stored in an address, the program can access the memory of the corresponding value and operation according to the saved address, such as:

Where the address is 0x00000016 is an address, point to address 0x00000036, that is, 0x00000016 is opened as a pointer variable, you can refer to the value of the 0x00000036 address, which is accessed by reference to the variable; another way to access variables is by value access, This is the 0x00000008 address in the figure.

There are two types of data in javascript: the base type and the reference type. Where the base type accesses the variable by value, the reference type accesses the variable by reference (and JavaScript does not allow direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly.) When you manipulate an object, you are actually manipulating the object's reference instead of the actual object.

Because the basic types and reference types are accessed differently, there are two operational differences, namely " pass parameters " and " copy variable values ". .

Okay, back to the subject. In general , There are two ways of passing parameters: passing by value and by reference. So what's the difference between these two ways?

    • passed by value , that is, the value of the variable is passed directly into the function's arguments. For example, there is a string primitive type variable test that has a value of "Hello" (that is, var test = "Hello"), which is passed the value "Hello" when passing the parameter, so no matter how the parameter is manipulated in the function does not affect the variable test. Such as:

    • passed by reference , passing the address of the variable to the parameters of the function. Because JavaScript does not exist by reference, so I give an example of the C language, assuming that there is a function void test (int *a) {*a = *a + 10;}, so called "int b = 10;test (&B) in the main function;", at this point, In the parameter pass of the test function, the address of B is passed to the parameter a of the test function, so the operation of the parameter in the function can be reflected outside the function. Such as:

As you can see here, you may find that there is an obvious difference between passing by value and passing by reference: when passing by value, manipulating parameters in a function does not affect variables that are outside of the function, and is affected when passed by reference.

As with JavaScript, it is known that when you pass a variable of a reference type, manipulating the arguments in the function affects the corresponding variables outside the function ! As a result, some small partners think that the reference type is passed by reference. Unfortunately,there is no pass by reference in JavaScript, and all parameters are passed by value !

So the question is, why do reference types use by value to pass on variables that are outside the function?

This is because the variable of the reference type is a reference, its value is the address of the object in the heap memory, and the value passed when the value is passed is an address, so manipulating the arguments in the function affects the corresponding variables outside the function .

Pass-by-value delivery of function parameters in JavaScript and pass-by-reference (that is, pass by address)

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.