JavaScript---Parameter: Cannot pass reference type

Source: Internet
Author: User
Tags call by reference

Summarize:

• Pass-by-value (call by value) is the most common evaluation strategy: a function's formal parameter is a copy of the argument that was passed when it was called. Modifying the value of a parameter does not affect the argument.

• When passed by reference (call by reference), the function's formal parameter receives an implicit reference to the argument, not the copy. This means that if the value of a function parameter is modified, the argument is also modified. Both points to the same value.


1. Passing the value of the base type

    var box= ' Lee ';                    // Pass the value of the base type, the value of the parameter is independent    of the value outside function One (box) {        box=123;    }    One (box);    alert (box);                 // Lee

Answer: This should all be understood, the formal parameter does not affect the actual argument.

2. Passing reference types (note that they are not passed by reference type)

1     varbox=NewObject ();//Pass reference type, not pass by reference, value of parameter is address2     functionOne (box) {3Box.name= ' Lee ';4         varbox=NewObject ();5Box.name= ' KKK '6     }7 One (box)8alert (box.name);//Lee

Answer: Here you may have doubts, why not KKK. First, the function argument is a reference type that passes its address, that is, a copy, all points to the same value, except that the address is different, and then the value is found along the address. Line 3rd, add the Name property, line 4th
New object, when the box points to the new value, the assignment KKK no longer affects the box value outside the original function, because the address points to a different value.

Note: By reference, the entire box object is passed in, is not a copy, executes line 4th, the previous box value will be overwritten, and box.name= ' KKK '.

conclusion: for numbers, strings, etc., they are passed to the function parameter, and the change of function parameter does not affect the variables outside the function.
For arrays and objects, etc., the value of the variable of the object (array) is passed to the function parameter, which holds the address to the object (array).
When the function changes the contents of the object (array) that the address points to, it also changes the contents of the object (array) to which the function external variable points, and when the function changes the address of the variable,
The actual relationship with the variables outside the function is lost and becomes a completely different object, without causing any changes to the external objects of the function.


The base type value is passed the value, directly accesses the value of the stack
The reference type passes the address, and if the address within the function is not changed, the actual participant changes as the function executes, and if the address changes, it is no longer related to the argument

JavaScript---Parameter: Cannot pass reference type

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.