【问题标题】:difference between this.some = undefined Vs delete this.some [duplicate]this.some = undefined 与删除 this.some [重复] 之间的区别
【发布时间】:2019-06-02 07:37:45
【问题描述】:

嘿,我正在阅读delete operator 的文档,但我看不到任何显着的性能差异(在大多数用例中):

const myObj = {
    name: 'Juan',
    last: 'Molina'
};

delete myObj.last;    // myObj.last = undefined
console.log(myObj);   // {name: 'Juan'} 

我发现的唯一区别是 delete 命令区分了自己的属性和链属性,但我认为这是一种特定情况。

【问题讨论】:

标签: javascript


【解决方案1】:

delete 就像名字所说的那样:它从对象中删除属性。

与此相反,简单地将undefined 分配给该属性不会删除它

const myObj = {
    name: 'Juan',
    last: 'Molina'
};

myObj.last = undefined
console.log(myObj);   // { "name": "Juan", "last": undefined }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 2019-03-05
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2013-04-08
    相关资源
    最近更新 更多