简体   繁体   English

JavaScript - 如何/可以从函数中将对象引用设置为null?

[英]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 . 我希望onull的原因是后来我使用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.

相关问题 如何在 Javascript 中将日期 object 设置为 null? - How can I set Date object to null in Javascript? 如何在Javascript / jQuery中将对象引用传递给函数? - How can I pass an object reference to a function in Javascript / jQuery? 如何从javascript中返回的对象引用私有方法? - How can i reference a private method from returned object in javascript? 我可以通过将Javascript对象的映射值设置为null来将自身从内存中删除吗? - Can I have a javascript object remove itself from memory by having it set its map value to null? 在Javascript中,如何从该函数中调用但在其他位置定义的函数中引用函数范围的变量? - In Javascript, how can I reference a function-scoped variable from a function called within that function but defined elsewhere? 如何在对象函数中引用对象数据? - How can I reference object data within an object function? 如果变量中有名称,如何引用 JavaScript object? - How can I reference a JavaScript object if I have the name in a variable? 如何将JavaScript对象的值设置为null - How do I set a JavaScript object's value to null 如果Javascript对象具有保存对函数的引用的属性,是否可以从函数本身中获取该属性名称? - If a Javascript object has a property which holds a reference to a function, can I get that property name from within the function itself? 如何从onclick字符串引用JavaScript对象函数 - How to reference javascript object function from onclick string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM