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?