About JavaScript functions and their parameters

Source: Internet
Author: User

Origin of the problem:

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title>Document</title>67<body>8     9</body>Ten<script type= "Text/javascript" > One     varA = []; A  -     functiontest1 (aa) { -         varb = 3; the Aa.push (b); -Console.log (' AA: ' + AA);//[A] -     } -  + Test1 (a); -  +Console.log (a);//[A] A  at /************************************/ -     varc = 1; -  -     functiontest2 (cc) { -         varD = 3; -CC = CC +D; inConsole.log (' cc: ' + cc);//4 -     } to  + test2 (c); -  theConsole.log (c);//1 *  $</script>Panax Notoginseng

You can see that when an incoming parameter is an array, the function is called, the value of the parameter changes, but the value of the parameter is unchanged when the number is passed in.

The cause of the problem is as follows:

The value of the JS function and the parameters of the transmission

Pass value: Just pass the value of the variable into the function, the function will also configure memory to save the parameter value, so it does not change the value of the original variable.

Address: The memory location where the variable is actually saved is passed into the function, so if the value of the parameter is changed in the function, the value of the original parameter is also changed.

Numeric, String, and Boolean-----pass values

objects, arrays, and functions----address

String Object--Address

Original address: http://www.cnblogs.com/Mblog/archive/2009/12/24/1631215.html

Workaround: The function ends with return

About JavaScript functions and their parameters

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.