1

I want to pass an object or array to a function, make it undefined, and see the changes after the function execution ends.

var arr = ['aaa', 'bbb', 'ccc'];
var reset = function (param) {
   param[0] = 'bbb';
   param = undefined;
}
reset(arr);

All right, so the result is ['bbb', 'bbb', 'ccc'], but I want it to be undefined. Is it possible to do this efficiently?

user99999
  • 1,994
  • 5
  • 24
  • 45
  • Define "efficiently". Also, you can't really destroy a passed in object in JavaScript (arrays are objects). – VLAZ Sep 22 '16 at 22:53
  • It is imposible using pure javascript. However, you can create a native function in C++ which will delete the object whose reference is passed to it. It will work only in Node.js and it will work only for objects, functions and symbols. –  Sep 22 '16 at 22:56

2 Answers2

5

JavaScript is a pass by value language, so modifying the value of a function parameter inside the function cannot have an effect on the variable passed as the argument.

If you want to do something like that, you can have your function return the value:

var reset = function(param) {
  // think think think
  if (whatever)
    return undefined;
  return param;
};

arr = reset(arr);

Now, if the function decides that the right thing to do is empty out the source variable, it returns undefined.

If you just want to clear the variable, however, there's no need for a function:

arr = undefined;
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

You can't pass a variable by reference in JavaScript. What you can do instead, if the variable is in the same or greater scope than reset(), is use the variable itself inside the function as shown below:

var
  arr = ['aaa', 'bbb', 'ccc'],
  reset = function () {
    arr = undefined;
  }

reset();
console.log(arr);

Or instead you can just make it equal to undefined:

var arr = ['aaa', 'bbb', 'ccc'];
arr = undefined;
console.log(arr);
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
  • You can pass by objects by reference in Javascript. https://jsfiddle.net/8cjsd8ye/. Or am I missing something. – Leeish Sep 22 '16 at 23:03
  • @Leeish you're missing something :) The term "pass by reference" is not about passing references to objects; it's old terminology that really predates that concept. JavaScript is purely pass by value; it just happens that object references are a certain kind of value. If you look at the OP closely and think about whether what is being asked is possible, you'll see the distinction. – Pointy Sep 22 '16 at 23:05
  • @Leeish at best you can call what JS does pass-by-copy-of-a-reference. But Pointy is correct, the _value_ of the object is that "copy of a reference". – VLAZ Sep 22 '16 at 23:09
  • So in my example. If the obj was copied. How do I reference the original value of a? – Leeish Sep 22 '16 at 23:17
  • @Leeish the important difference would be if your function was trying to change `obj` and not a property of `obj`, as is the case in the question here. You can change properties of an object passed in, but you cannot touch a variable in some external context that was passed in as an argument. That's what *pass by reference* means - it's kind-of weird, and very few modern languages do that. – Pointy Sep 22 '16 at 23:21
  • So it makes a copy but the original is still Mutable. Wtf. – Leeish Sep 22 '16 at 23:22
  • 1
    I don't understand that question. The object is not "copied". You get a "copy" of the reference. Notice the quotes. More accurately, the name of this is **call-by-sharing**. When you pass in an object to a function, any _modification_ is done to the actual object that exists outside the function. The reference is thus _shared_. You cannot, however, wipe the object like so `obj = null` as that assigns your variable to point to a new thing, rather than setting the object. – VLAZ Sep 22 '16 at 23:24
  • 1
    I really wish that sometime back in about 1972 the terms *pass by value* and *pass by reference* had been replaced by something like "Cambridge Parameters" and "Wallace-Humphreys Parameters". It'd make this so much easier to discuss. – Pointy Sep 22 '16 at 23:35
  • http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language. The real problem is tons of people, myself included till now have read and been taught that JS is pass by reference for complex objects. It's simply not, and I've never read call-by-sharing until today. I've been doing this a while. – Leeish Sep 23 '16 at 14:25
  • 1
    I did not know that complex objects in javascript are not values but simply references themselves. I guess I never cared to think that deeply about it but now it makes sense. Some of the comments in my above link to a different SO post explained it very well. I didn't understand what you meant when you said `"copy" of a reference` until I understood the object was just a reference of several primitives the whole time. – Leeish Sep 23 '16 at 14:35
  • @Leeish don't feel bad; the terminology is weak, and honestly there are very few pass-by-reference languages in common use. (I don't know about modern FORTRAN but back in the day it was the poster child pass-by-reference language.) – Pointy Sep 23 '16 at 18:10