Analyze JavaScript value passing

Source: Internet
Author: User

We all know that JavaScript contains basic and reference type values. I will share the applications of these two types in variable interaction. I have practiced and encountered problems ~~~~

1. Value of the value assignment variable:

[1]Basic Type Value

VaR num = 5;

VaR num1 = num;

The actual method is to copy a copy of num 5 and assign it to num1. Although they are equal, the values of the two variables are completely independent.

Verification:

Alert (num1); // 5

Alert (Num + 5); // 10

[2]Reference Type

When a variable is assigned a reference type to another variable, a copy is also copied to another variable, but here the pointer is copied, this pointer points to an object in the stack.

If one of the variables changes, the other also changes. See:

VaR obj1 = new object ();

VaR obj2 = obj1;

Obj1.name = "XXX ";

Alert (obj2.name); // "XXX"

2. parameter transfer

[1]The basic type value is passed by value.

If a variable transmits a value to a function parameter, the passed value is copied to the parameter (the arguments element of ecmascept ),

Function addnum (Num ){

Num = num + 10;

Return num;

}

VaR num = 10;

VaR result = addnum (Num );

Alert (Num); // 10 is not changed

Alert (result); // 20

[2] The reference type is still passed by value !!!!!

Function setname (OBJ ){

OBJ. Name = "XXX ";

}

VaR man = new object ();

Setname (OBJ );

Alert (man. Name); // "XXX"

HereNever think it is a reference TransferIf you think so, you have to look at the followingCode:

Function setname (OBJ ){

OBJ. Name = "XXX ";

OBJ = new object ();

OBJ. Name = "wrong ";

}

VaR man = new object ();

Setname (man );

Alert (man. Name); // or "XXX"

The example above demonstrates that if the value is passed by reference, the result should be "wrong ".

In fact, declaring an OBJ inside the function is only a local object, and the object will be destroyed after the function is executed.

 

Due to my limited level, I cannot cite some high-end examples. Sorry.

If you think there is something wrong or incomplete, please correct it and try again.

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.