我有点搞不懂JavaScript的删除操作符。以下面这段代码为例:

var obj = {
    helloText: "Hello World!"
};

var foo = obj;

delete obj;

执行这段代码后,obj为空,但foo仍然引用与obj完全相同的对象。我猜这个对象就是foo指向的那个对象。

这让我很困惑,因为我以为写delete obj删除的是obj在内存中所指向的对象——而不仅仅是变量obj。

这是因为JavaScript的垃圾收集器是在保留/释放的基础上工作的,所以如果我没有任何其他变量指向对象,它将从内存中删除?

(顺便说一下,我的测试是在Safari 4中完成的。)


当前回答

我们有多种方法来删除Object属性。

箭头函数:我们还可以使用箭头函数从对象中删除属性,这是一个单行解决方案。

const obj = {
    'first': 'one',
    'second': 'two',
    'third': 'three'
}

const fn = (key, { [key]: deletedKey, ...others }) => others;

console.log(fn('first', obj))        // { 'second': 'two', 'third': 'three' }

Reduce方法:在javascript中我们可以使用Reduce方法从原始对象中删除特定的属性。

const obj = {
    'first': 'one',
    'second': 'two',
    'third': 'three'
}

const exceptSecond = Object.keys(obj).reduce((acc, key) => {
    if (key !== 'second') {
        acc[key] = obj[key]
    }
    return acc
}, {})

console.log(exceptSecond)    // { 'first': 'one', 'third': 'three' }

删除:这是一种简单的删除方法。

delete obj.first; 
// Or
delete obj['first'];

使用unset,省略,Pic方法从"loadash" lib:

    import { unset } from 'lodash'


const obj = {
    'first': 'one',
    'second': 'two',
    'third': 'three'
}

unset(obj, 'third')        // true

console.log(obj)        // { 'first': 'one', 'second': 'two' }

// Using omit 
import { omit } from 'lodash'
const obj1 = {
    'first': 'one',
    'second': 'two',
    'third': 'three'
}

omit(obj1, [ 'first', 'second' ])

console.log(obj1)  

反射删除属性:这是ES6中引入的新的内置对象。现在可以通过调用这个Refect对象的deleted property()函数来删除对象属性。

这个函数等价于我们在第一个方法中讨论的delete操作符。

const someObject = {
    'first': 'one',
    'second': 'two',
    'third': 'three'
}

Reflect.deleteProperty(someObject, 'second')

console.log(someObject)        //  { 'first': 'one', 'third': 'three' }

其他回答

Setting a variable to null makes sure to break any references to objects in all browsers including circular references being made between the DOM elements and Javascript scopes. By using delete command we are marking objects to be cleared on the next run of the Garbage collection, but if there are multiple variables referencing the same object, deleting a single variable WILL NOT free the object, it will just remove the linkage between that variable and the object. And on the next run of the Garbage collection, only the variable will be cleaned.

如果你想根据对象的值删除它,可以这样做:

Object.keys(obj).forEach((key) => {
  if (obj[key] === "Hello World!") {
    delete obj[key];
  }
});

但是删除对象并不是一个好主意。设它为undefined,这样当你把它传递给一个参数时。它不会显示出来。无需删除。

Object.keys(obj).forEach((key) => {
  if (obj[key] === "Hello World!") {
    obj[key] = undefined;
  }
});

delete命令对常规变量无效,只对属性有效。在删除命令之后,属性没有值null,它根本不存在。

如果属性是对象引用,则delete命令删除属性,但不删除对象。如果该对象没有其他引用,则垃圾收集器将处理该对象。

例子:

var x = new Object();
x.y = 42;

alert(x.y); // shows '42'

delete x; // no effect
alert(x.y); // still shows '42'

delete x.y; // deletes the property
alert(x.y); // shows 'undefined'

(在Firefox中测试。)

delete操作符从数组中删除对象、对象的属性或元素。操作符还可以删除未使用var语句声明的变量。 在下面的例子中,'fruits'是一个声明为var的数组,并被删除了(真的吗??)

delete objectName
delete objectName.property
delete objectName[index] 
delete property // The command acts  only within a with statement.

var fruits = new Array("Orange", "Apple", "Banana", "Chery");
var newParagraph = document.createElement("p"); 
var newText = document.createTextNode("Fruits List : " + fruits);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
//Delete the array object.
delete fruits;
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("Display the Fruits after delete the array object - Fruits List : "+ fruits;); 
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);

https://www.w3resource.com/javascript/operators/delete.php

在寻找同样的答案时,我偶然发现了这篇文章。我最终所做的只是弹出obj.pop()我的对象中所有存储的值/对象,这样我就可以重用对象。不知道这是不是不好的做法。这个技巧在我用Chrome Dev工具或FireFox Web Console测试代码时派上了用场。