简体   繁体   中英

what is different between call function with parameters and call function with return value of other parameters

if I have function like this

function cloneObj( obj ) {
    return JSON.parse( JSON.stringify( obj ) );
}



function test( obj ) {
    console.log(obj);
}

var x = {1:2};

what is different between call function by this way

 test(cloneObj(x)); 

or call this function by this way

var y = cloneObj(x); 
test(y);

No difference at all, other than in the second example you create a y variable and keep a reference to the cloned object in it, and in the first you don't (once your code is complete, the cloned object no longer has any strong 1 references to it and can be garbage-collected).


1 The console keeps a reference to the object when you log it, but I assume it's a weak reference that doesn't prevent GC (and it is an assumption, not something I know for a fact).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM