[英]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.
基本上我有一些对象,我传递给一个函数,在某些条件下,我希望该函数将对象设置为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. 不幸的是,我似乎只能将函数的参数设置为null。 After calling the function 'o' will still refer to an object.
调用函数'o'后仍然会引用一个对象。
So, is it even possible to do this? 那么,甚至可以这样做吗? And if so, how?
如果是这样,怎么样?
If you want to change the value of o
when f(o)
is called, you have two options: 如果要在调用
f(o)
时更改o
的值,则有两个选项:
1) You can have f(o)
return a new value for o and assign that to o like this: 1)你可以让
f(o)
为o返回一个新值,并将其分配给o,如下所示:
var o = {'val' : 0};
o = f(o);
// o == null
Inside of f()
, you return a new value for o
. 在
f()
,为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()
. 2)将o放入另一个对象并将对该容器对象的引用传递给
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. javascript语言没有像C / C ++那样的真正指针,它允许您将指针传递给变量,然后通过该指针返回以从函数中更改该变量的值。 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.
对象和数组通过引用传递,因此您可以从函数返回到原始对象。
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);
JavaScript always passes function arguments "by value". JavaScript总是“按值”传递函数参数。 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.
因此,虽然您无法将外部变量设置为null或另一个对象,但您可以修改该对象的内容。 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. 但我建议使用James Montagne建议让函数返回一个对象或null并将结果返回给你的变量。
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.
尽管包装器的答案有效,但它很难看,而且我的函数是异步的,所以我不能返回null值。
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
. 我希望
o
为null
的原因是后来我使用if (o) //do something
,这可能是OP和其他人想要将其设置为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
The easiest way of doing it would be to create a global variable called NULL and set it to null 最简单的方法是创建一个名为NULL的全局变量并将其设置为null
var NULL = null
Then you can set an existing variable to NULL without modifying the original object. 然后,您可以将现有变量设置为NULL,而无需修改原始对象。
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.