javascript--functions-Passing by value

Source: Internet
Author: User

Pass-by-value (byvalue): When you assign a value between two variables or pass a variable as an argument to a function, the value in the original variable is assigned to the other party (the new variable).
Value for Primitive type: Modify new variable without affecting the original variable
object to reference type: Modifying an object with a new variable is equivalent to modifying the original object directly.

First example: Values of the original type

1     var a=10; 2     var b=a; // Take out the value in a and copy one to B. 3     a++; 4     Console.log (a); //  One 5     Console.log (b); // Ten

Second example: Objects of a reference type

1. Functions

1     var card=10; 2     function Buy (Card) {3       card-=3; 4       Console.log (card); 5     }6     Buy (card);   77     console.log (card);   Ten

2. Arrays

1     varmy=["Package", "Package", "Package", "Package", "package"];2          //0 1 2) 3 43     varIpxmy;4Lp[0]= "";5Console.log (String (my));//46My[4]= "";7Console.log (String (LP));//38 9     vararr=[1,2,3,4,5];Ten            //0 1 2) 3 4 One     functionFun (arr) { AArr[5]=6; -arr=[1,2,3]; -Arr[6]=12; the Console.log (arr); -     } -Fun (arr);//123,,, -Console.log (arr);//123456

javascript--functions-Passing by value

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.