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

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

var foo = obj;

delete obj;

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

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

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

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


当前回答

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

其他回答

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测试代码时派上了用场。

除了GC问题之外,对于性能,应该考虑浏览器可能在后台执行的优化—>

http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/

这似乎是更好的空引用比删除它,因为这可能会改变幕后的'类' Chrome使用。

刚刚找到一个jsperf,你可能认为有趣的在这个问题上。(把它放在周围可以方便地完成图片)

它比较了delete、设置null和设置undefined。

但请记住,当您多次删除/设置属性时,它会测试情况。

我们有多种方法来删除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' }