is a JavaScript function parameter pass a value pass or a reference pass?

Source: Internet
Author: User

On the Internet for JS function parameter transfer way of opinions, here is also on the Internet to choose a better explanation:

JS and Java are more similar, understand the Java parameter delivery way to understand JS is not difficult.

If the value of a variable is an absolute basic type variable such as:

function Add (num) {     num+=10;      return num;  }  Num=10;  Alert (Add (num));  AELRT (num);   // Output 20,10  

This result should be meaningless to everyone, the value of the basic type variable itself is immutable, change is the parameter address. Even if the values of variables A and B are all 10, there is no relationship between them.

Then the object's reference is passed:

function setName (obj) {      Obj.name= "Ted";  }   var obj=New  Object ();  SetName (obj);  alert (obj.name);   // output Ted  

The code above first creates an object and then assigns its reference to the parameter obj of the function setname, so that changes to the parameter inside the function affect the argument.

And then looking at the code

function setName (obj) {      Obj.name= "Ted";      obj=new  Object ();      Obj.name= "Marry";  }   var obj=New  Object ();  SetName (obj);  alert (obj.name);   // output Ted  

The difference in this example is that the parameter is given a new value within the function, which results in a new memory address pointing to the parameter, so that the formal parameter and the argument are out of the relationship.

Original address: http://bestchenwu.iteye.com/blog/1076557

is a JavaScript function parameter pass a value pass or a reference pass?

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.