JS function to pass the data structure of parameters

Source: Internet
Author: User

JS data is divided into basic types (number, string, Boolean, null, undefined) and reference types (Object, Array, Data, REGEXP, function, etc.) two. The value of the base type variable is itself, where a=3, the value of a is 3, and the value of the reference type's variable obj={b:1},obj itself is the address of the store {b:1}, and {b:1} is the content it points to.

When you pass a value, both of your own value (A is 3,obj is the address) copies a copy to the formal parameter, note that {b:1} is still only one, but at this point, there are two reference types of variables that point to it, you can modify the contents of {b:1} by any one, but if you modify the point of any reference variable (its own value) , the point of another reference variable does not change.

var obj={a:1,b:2}; Reference type
var v=0;  Basic type
function test (arg,v) {
    arg.a=2;    The value of the outer obj.a can be overwritten because at this point arg and obj are pointing to the same
    arg={};        The original obj is not equal to {}
    arg.a=1;    The value of OBJ.A cannot be overwritten because at this point arg points to the changed

    v=100;  The value of V can not be changed
}
Test (obj,v);
Console.log (obj); {a:2, b:2}
Console.log (v);//0
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.