删除 a.x 与 a.x = 未定义

做这两件事有什么实质性的区别吗?

delete a.x;

a.x = undefined;

哪里

a = {
x: 'boo'
};

可以说它们是等价的吗?

(我没有考虑像 “ V8不喜欢使用 delete更好”这样的东西)

30071 次浏览

它们是不等价的,主要区别在于设置

a.x = undefined

意味着 a.hasOwnProperty("x")仍将返回 true,因此它仍将在 for in循环和 Object.keys()循环中显示。然而呢

delete a.x

意味着 a.hasOwnProperty("x")将返回 false

通过测试无法判断属性是否存在

if (a.x === undefined)

如果要确定属性是否存在,应始终使用

// If you want inherited properties
if ('x' in a)


// If you don't want inherited properties
if (a.hasOwnProperty('x'))

在原型链 (由 哇 ~ ~ ~ ~ ~ ~ ~ ~ 提到)之后,调用 delete将允许它沿着原型链上行,而将值设置为未定义将不会在链式原型中查找属性

var obj = {
x: "fromPrototype"
};
var extended = Object.create(obj);
extended.x = "overriding";
console.log(extended.x); // overriding
extended.x = undefined;
console.log(extended.x); // undefined
delete extended.x;
console.log(extended.x); // fromPrototype

删除继承属性 如果要删除的属性是继承的,delete将不会影响它。也就是说,delete只删除对象本身的属性,而不删除继承的属性。

var obj = {x: "fromPrototype"};
var extended = Object.create(obj);
delete extended.x;
console.log(extended.x); // Still fromPrototype

因此,如果需要确保对象的值是未定义的,那么在继承属性时 delete将无法工作,在这种情况下,必须将其设置(重写)为 undefined。除非检查它的地方将使用 hasOwnProperty,但是假设所有检查它的地方都将使用 hasOwnProperty可能并不安全

解释一下这个问题:

delete a.xa.x = undefined是等价的吗?

没有。

前者从变量中删除键,后者将键的值设置为 undefined。这在迭代对象的属性和使用 hasOwnProperty时产生了很大的不同。

a = {
x: true
};
a.x = undefined;
a.hasOwnProperty('x'); //true
delete a.x;
a.hasOwnProperty('x'); //false

此外,当涉及到原型链时,这将产生显著的差异。

function Foo() {
this.x = 'instance';
}
Foo.prototype = {
x: 'prototype'
};
a = new Foo();
console.log(a.x); //'instance'


a.x = undefined;
console.log(a.x); //undefined


delete a.x;
console.log(a.x); //'prototype'

这个来自节点的 REPL 应该说明这种差异。

> a={ x: 'foo' };
{ x: 'foo' }
> for (var i in a) { console.log(i); };
x
undefined
> a.x=undefined;
undefined
> for (var i in a) { console.log(i); };
x
undefined
> delete a.x;
true
> for (var i in a) { console.log(i); };
undefined

名称有点混乱。 a.x = undefined只是将属性设置为 undefined,但该属性仍然存在:

> var a = {x: 3};
> a.x = undefined;
> a.constructor.keys(a)
["x"]

delete实际上删除了它:

> var a = {x: 3};
> delete a.x;
> a.constructor.keys(a)
[]

是的,这是有区别的。 如果使用 delete a.x,x 不再是 a 的属性,但是如果使用 a.x=undefined,它是属性,但是它的值是未定义的。

我相信你能看出 var o1 = {p:undefined};var o2 = {};之间的区别。

在这两种情况下,o.p都是 undefined,但是在第一种情况下,这是因为它是 价值,而在第二种情况下是因为 没有价值

delete是一个操作符,它使您可以从 o1(或者另一个对象,它的 p属性有一个值)获得 o2: delete o1.p;

反向操作是通过简单地为属性 o1.p = undefined;分配一个值(在本例中是 undefined,但可能是其他值)来完成的。

所以 没有,它们是不等价的。


delete o.p;会的

  • 如果对象有属性,则从该对象中删除属性 p

  • 什么都别做

o.p = undefined;会的

  • 如果对象还没有属性,那么将属性 p添加到对象中,并将其值设置为 undefined

  • 如果对象已经拥有属性,则只需更改该属性的值


从性能的角度来看,delete很糟糕,因为它是 修改对象的结构(就像在构造函数中没有初始化它的情况下添加一个新属性一样)。

而将值设置为 undefined也会释放内容,但不会强制修改结构。

对象只是一个树表示,这意味着,在内存中,根指向存储该对象的键的各个内存位置。该位置指向存储该键的实际值的另一个位置,或存储子键的位置或存储数组值的位置。

当您使用 delete 从对象中删除任何键时,实际上它会删除该键与其父对象之间的链接,并释放键及其值的内存位置以存储其他信息。

当您尝试通过将未定义的键设置为其值来删除任何键时,那么您只是设置了它的值,而不是删除了该键。这意味着如果键未定义,键内存位置仍然与其父对象和值链接。

不使用 delete 关键字而使用 unDefinition 是一种糟糕的做法,因为它不释放该关键字的内存位置。

即使键不存在,并且您将其设置为未定义的,那么该键将以值 undefined创建。

例如:。

var a = {};
a.d = undefined;
console.log(a); // this will print { d: undefined }

Delete 无法与继承的属性一起工作,因为该属性不是该子对象的一部分。

如果 a.x是 setter 函数,则 a.x = undefined将调用该函数,而 delete a.x将不调用该函数。

使用数组而不是对象,我可以展示 delete 比未定义的使用更少的堆内存。

例如,此代码将无法完成:

let y = 1;
let ary = [];
console.log("Fatal Error Coming Soon");
while (y < 4294967295)
{
ary.push(y);
ary[y] = undefined;
y += 1;
}
console(ary.length);

它产生了这样一个错误:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.

因此,正如您所看到的,undefined实际上占用了堆内存。

但是,如果你也 delete的元项目(而不是只是设置为 undefined) ,代码将缓慢完成:

let x = 1;
let ary = [];
console.log("This will take a while, but it will eventually finish successfully.");
while (x < 4294967295)
{
ary.push(x);
ary[x] = undefined;
delete ary[x];
x += 1;
}
console.log(`Success, array-length: ${ary.length}.`);

这些都是极端的例子,但他们提出了一个关于 delete的观点,我没有看到任何人提到任何地方。