Arkia
Arkia

Reputation: 1693

JavaScript - How/Can I set an object reference to null from a function?

I'm wondering if this is even possible. Basically I have a couple objects that I pass to a function, and under certain conditions I want that function to set the object to null.

Ex.

var o = {'val' : 0};

f = function(v)
{
   v = null;
};

f(o);   // Would like this to set 'o' to null

Unfortunately it seems I can only set the function's argument to null. After calling the function 'o' will still refer to an object.

So, is it even possible to do this? And if so, how?

Upvotes: 14

Views: 35223

Answers (5)

jfriend00
jfriend00

Reputation: 707876

If you want to change the value of o when f(o) is called, you have two options:

1) You can have f(o) return a new value for o and assign that to o like this:

var o = {'val' : 0};
o = f(o);
// o == null

Inside of f(), you return a new value for o.

function f(v) {
    if (whatever) {
        return(null);
    }
}

2) You put o into another object and pass a reference to that container object into f().

function f(v) {
    if (whatever) {
        v.o = null;
    }
}

var c = {};
c.o = {'val' : 0};

f(c);
// c.o == null;

The javascript language does not have true pointers like in C/C++ that let you pass a pointer to a variable and then reach back through that pointer to change the value of that variable from within the function. Instead, you have to do it one of these other two ways. Objects and arrays are passed by reference so you can reach back into the original object from the function.

Upvotes: 14

Sivx
Sivx

Reputation: 354

The easiest way of doing it would be to create a global variable called NULL and set it to null

var NULL = null

Then you can set an existing variable to NULL without modifying the original object.

var NULL = null;
var original = {};
var ptr = original;
ptr = NULL;
console.log("original is not null = " + (original!==null));
console.log("ptr is null = " + (ptr===null));

Upvotes: 0

willlma
willlma

Reputation: 7543

I came here with the same problem. Although the wrapper answer works, it's ugly, and my function is asynchronous, so I can't return a null value.

The reason I wanted o to be null is that later I use if (o) //do something, which might be why OP and others want to set it as null. As such, my solution, although not an answer to the exact question, was

var o = {'val' : 0};
f = function(v) {
  v.isNull = true;
}
if (!o.isNull) // do something

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150080

JavaScript always passes function arguments "by value". Which means a function can't change what the variable outside the function points to.

However, when you pass an object to a function the "value" that is passed is a reference to the actual object, not a copy of the object. So although you can't set the outside variable to null or to another object you can modify the contents of that object. Which means you can do something like this:

var containerObj = {'o' : {'val' : 0} };

f = function(v) {
  v.o = null;
};

f(containerObj.o);   // This property would be set to null successfully.

Obviously creating a bunch of container objects just so you can pass them to functions isn't very pretty, but it is one way to do it.

But I'd recommend going with James Montagne's suggestion of having the function return an object or null and assigning the result back to your variable.

Upvotes: 5

James Montagne
James Montagne

Reputation: 78710

Not sure exactly the real use of this, but if you restructured a bit, you could do this:

var o = {'val' : 0};

var f = function(v)
{
   if(something){
      return null;
   }else{
      return v;
   }
};

o = f(o);

Upvotes: 3

Related Questions