我有一个类似于下面函数的函数。它需要一个名为
link
,调用 getAdminParams
,然后使用该调用的返回值
更改链接对象中的属性:
function checkParams(link: Link) {
var rtn : IAdminParams = null,
table = null;
if (link.Action === "Create") {
if (link.Params == null) {
rtn = getAdminParams(link.Entity);
if (rtn.Success) {
link.Url = link.Href + rtn.Param;
table = rtn.Table;
} else {
link.$Link.prop('disabled', false);
return;
}
} else {
link.Url = link.Href + link.Params;
table = link.Entity;
}
} else {
link.Url = link.Href;
}
}
我调用函数如下。
function adminDialog($link: JQuery) {
var link = new Link($link);
checkParams(link);
doDialogAjax(link);
}
当我将链接的值传递给 checkParams(link)
时,它将被传递
引用?换句话说,我在 checkParams(link: Link)
中所做的更改
函数对链接对象可用,因此它们可以在 doDialogAjax
中使用
功能?
最佳答案
是的,也不是。
"When I pass the value of link to the checkParams(link) will it be passed by reference?"
没有。
"Will the changes I make in the checkParams(link: Link) function be available to the link object so they can be used in the doDialogAjax function?"
是的。
变量不是通过 引用传递的。它是按值传递的,但值是一个引用。
如果变量是通过引用传递的,则函数可以更改变量。那不会发生:
function change(obj) {
// change the object
obj.value = 42;
// replace it with a new object
obj = { value: 1 };
}
// create an object
var a = { value: 0 };
// create another variable, to use in the call to the function
var b = a;
change(b);
alert(a.value); // shows 42, as the function changes the object passed in
alert(b.value); // shows 42, not 1, as the variable b is not changed by the function
如果变量是通过引用传递的,b
将被函数中创建的新对象替换,b.value
将是 1
.
关于javascript - 当我将对象传递给 JavaScript 函数时,该对象是否通过引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13344394/