Javascript - 如何清空底层引用?

发布于 2024-12-02 10:12:47 字数 607 浏览 0 评论 0原文

给定这个 javascript 代码:

this.underlyingReference = {name: 'joe'};

this.nullMe(this.underlyingReference);

alert(this.underlyingReference.name);

function nullMe(variable) {
   variable = null;
}

Javascript 有没有办法让我使用“variable”来 null this.underlyingReference ?我希望能够清空变量并清空底层引用,而不是简单地清空对引用的引用。

我读过这样的文章 http://snook.ca/archives/javascript/javascript_pass关于 Javascript 的传递引用功能,但似乎当您想要销毁底层引用时,该行为并不是我对引用的期望。

当执行通过第二行代码时,我希望“this.underlyingReference”被清空。然而,警报线显示底层参考仍然存在并且正在运行。

given this javascript code:

this.underlyingReference = {name: 'joe'};

this.nullMe(this.underlyingReference);

alert(this.underlyingReference.name);

function nullMe(variable) {
   variable = null;
}

Is there a way in Javascript for me to null this.underlyingReference using "variable"? I want to be able to null out variable and have the underlying reference be nulled and not simply null the reference to a reference.

I've read articles like this one http://snook.ca/archives/javascript/javascript_pass about Javascript's pass by reference functionality but it appears as though when you want to destroy the underlying reference the behavior is not what I have come to expect from references.

when execution passes the second line of code I want "this.underlyingReference" to be nulled out. However the alert line shows that the underlying reference is still alive and kicking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿光 2024-12-09 10:12:47

为什么不直接将 null 分配给该属性:

this.underlyingReference = null;
alert(this.underlyingReference);// null

或者,如果您想销毁该属性,可以使用删除:

delete this.underlyingReference;
alert(this.underlyingReference);// undefined

如果您仍然想要进行函数调用,可以使用以下设置:

var NullMe = function(obj, propName) 
{
    obj[propName] = null;
    //OR, to destroy the prop:
    delete obj[propName];
}
NullMe(this, 'underlyingReference');

why not just assign null to that property:

this.underlyingReference = null;
alert(this.underlyingReference);// null

or, if you want to destroy the property, you can use delete:

delete this.underlyingReference;
alert(this.underlyingReference);// undefined

if you still want to have a function call, you can use this setup:

var NullMe = function(obj, propName) 
{
    obj[propName] = null;
    //OR, to destroy the prop:
    delete obj[propName];
}
NullMe(this, 'underlyingReference');
云巢 2024-12-09 10:12:47

你可以尝试

function nullMe(obj, reference){
    delete obj[reference];
}

nullMe(this, "underlyingReference");

或者

function nullMe(reference){
    delete this[reference];
}
nullMe.call(this, "underlyingReference");

You can try

function nullMe(obj, reference){
    delete obj[reference];
}

nullMe(this, "underlyingReference");

Or

function nullMe(reference){
    delete this[reference];
}
nullMe.call(this, "underlyingReference");
爱本泡沫多脆弱 2024-12-09 10:12:47

在某些情况下,Pascal 和 C 中使用的“旧引用”与 java、javascript 和最新编程语言中的“引用”之间存在混淆。

在 javascript 中,传递一个值,该值是对对象的引用。这意味着您可以更改该引用后面的对象,但不能更改引用本身。

如果您需要在方法中执行此操作,那么您需要“显式”执行此操作,例如:

this.nullMe("underlyingReference");
this.nullMe = function(name) {
    this[name] = null;
}

但是有一个方法来设置 null 有点过度设计:)

There is a confusion between the "old by reference", used in Pascal and in C in some cases, and the "by reference" in java, javascript and most recent programming languages.

In javascript, a value is passed, and that value is a reference to the object. That mean you can change the object following that reference, but not change the reference itself.

If you need to do that in a method, then you need to do it "explicitly", for example :

this.nullMe("underlyingReference");
this.nullMe = function(name) {
    this[name] = null;
}

But it's a bit, well, over-engineering to have a method to set null :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文