Reputation: 377
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);
Upvotes: 0
Views: 34
Reputation: 1074485
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 strong1 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).
Upvotes: 3