日期:2014-05-16  浏览次数:20449 次

Javascript的所有函数传参都是靠值参数

?

Javascript的所有函数传参都是靠值参数,而包括对象的传参,也是靠值传参,而不是

靠引用来传参。其实java方法的参数所有的也都是靠值参数的。passed by value,没

有靠引用传参。这一点很多人可能有误解的。

?

下面摘自(Professional.JavaScript.for.Web.Developers.2nd.Edition.pdf【javascrip高级程序设计】)

Argument Passing

All function arguments in ECMAScript are passed by value. This means that the value outside of the

function is copied into an argument on the inside of the function the same way a value is copied from

one variable to another. If the value is primitive, then it acts just like a primitive variable copy, but if the

value is a reference, it acts just like a reference variable copy. This is often a point of confusion for

developers because variables are accessed both by value and by reference, but arguments are passed

only by value.

When an argument is passed by value, the value is copied into a local variable (a named argument or, in

ECMAScript, a slot in the arguments object). When an argument is passed by reference, the location of

the value in memory is stored into a local variable, which means that changes to the local variable are

reflected outside of the function. Consider the following example:

function addTen(num) {
num += 10;
return num;
}
var count = 20;
var result = addTen(count);
alert(count); //20 - no change
alert(result); //30
?

Chapter 4: Variables, Scope, and Memory?83

Here, the function addTen() has an argument num , which is essentially a local variable. When called,

the variable count is passed in as an argument. This variable has a value of 20, which is copied into the

argument num for use inside of addTen() . Within the function, the argument num has its value changed

by adding 10, but this doesn ’ t change the original variable count that exists outside of the function. The

argument num and the variable count do not recognize each other; they only happen to have the same

value. If num had been passed by reference, then the value of count would have changed to 30 to reflect

the change made inside the function. This fact is obvious when using primitive values such as numbers,

but things aren ’ t as clear when using objects. Take this for example:

function setName(obj) {
obj.name = “Nicholas”;
}
var person = new Object();
setName(person);
alert(person.name); //”Nicholas”
?

In this code, an object is created and stored in the variable person . This object is then passed into the

setName() method, where it is copied into obj . Inside the function, obj and person both point to

the same object. The result is that obj is accessing an object by reference, even though it was passed into

the function by value. When the name property is set on obj inside the function, this change is reflected

outside the function because the object that it points to exists globally on the heap. Many developers

incorrectly assume that when a local change to an object is reflected globally, that means an argument

was passed by reference. To prove that objects are passed by value, consider the following modified code:

function setName(obj) {
obj.name = “Nicholas”;
obj = new Object();
obj.name = “Greg”;
}
var person = new Object();
setName(person);
alert(person.name); //”Nicholas”
?

The only change between this example and the previous one are two lines added to setName() that

redefin