function arguments for JavaScript (no reference pass only value pass)

Source: Internet
Author: User

var v1 = []
var v2 = {};
var v3 = {};
function foo (v1, v2, v3) {
V1 = [1];
v2 = [2];
V3 = {A:3}
}

Foo (v1, v2, v3);
Alert (v1); Blank
Alert (v2); [Object Object]
alert (V3.A); Undefined

/* This shows: v1, V2, V3 have not been changed, V1 is still an array of 0 elements, V2, V3 is still a blank object. (description is not a reference pass)
* However, arrays, objects, etc. are passed by value, which refers to the value of the variable address.
* The values of arrays, objects, etc., are passed by value differently than numbers or strings. Numbers, strings are copied directly into the value, and the array, object is to copy the variable address in the
* Before we let v1, V2, v3 as parameters into the function, there is a copy of the address, these address copies of the point and the outside of the V1, V2, v3 * Address point is the same. But we've assigned values for V1, V2, v3, which means we've changed the point of the address copy to a new array and object. Such internal *V1, V2, V3 and external v1, V2, v3 are completely broken.
* If we do not assign a new value, but manipulate it directly, then it will still be the same array or object that the outer v1, V2, V3 point to.
*/
var v1 = []
var v2 = {};
var v3 = {a:0};
function foo (v1, v2) {
V1.push (1);
v2.a = 2;
v3.a = 3;
}
Foo (v1, v2, v3);
Alert (v1); 1
alert (v2.a); 2
alert (V3.A); 3
Note that when a function with the same name is present, the previous one will be overwritten, and the overwrite occurs during the loading process, and the entire document is equivalent to just the next function at execution time.

function arguments for JavaScript (no reference pass only value pass)

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.