argumental reference inconsistency in javascript

断了今生、忘了曾经 提交于 2019-12-04 05:56:57

问题


I have recently encountered a nasty issue in JS.

Let say we pass a map, an array of objects to a function f.

var o=[{a:0}];
function f(a){
    for(var i in a){
        if (a.hasOwnProperty(i)){
            a[i]=null;
        }
    }
    return a;
};
var outp=f(o);

alert(outp[0]+" === "+o[0]+" : "+(outp[0]===o[0]));

// here we expect loose equality, and equality in type, 
//furthermore it should identically equal as well, and we got right!

But, we can not pass total responsibility of an object to a function as argument, same like in functional paradigm o=(function(o){return o})(), because any kind of modification to o is not referenced!

var o=[];
function ff(a){
    return (a=undefined);
};
var outp=ff(o);
alert(outp+" === "+o.constructor+" : "+(outp===o));
// here we expect true, but we got false!

Why is the above described reference loss and presumably different referencce handling in the second use case, though in both case, functions got the array argument in the 0. position?


回答1:


Javascript always passes arguments by value, so this won't work:

 function foo(x) {
    x = 100;
 }

 y = 5
 foo(y)
 y == 100 // nope

However this does work:

 function foo(x) {
    x.bar = 100;
 }

 y = {}
 foo(y)
 y.bar == 100 // yes

In the second snippet x is still passed by value, but this very value is a reference (pointer) to an object. So it's possible in a function to dereference it and access what's "inside" the object.



来源:https://stackoverflow.com/questions/15881736/argumental-reference-inconsistency-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!