两者中的任何一个有实质性的区别吗?
delete a.x;
与
a.x = undefined;
哪里
a = {
x: 'boo'
};
可以说它们相等吗?
(我没有考虑“ V8喜欢不使用delete
更好的东西”之类的东西)
它们不相等。主要区别在于设置
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'))
遵循原型链(由zzzzBov提及),调用delete
将允许它向上扩展原型链,而将值设置为undefined将不会在链接的原型中寻找属性http://jsfiddle.net/NEEw4/1/
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.x
和a.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'
如果a.x
是setter函数,a.x = undefined
则将调用该函数,而delete a.x
不会调用该函数。
是,有一点不同。如果使用delete a.x
x,则不再是a的属性,但是如果使用a.x=undefined
x是属性,但其值未定义。
名称有些混乱。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)
[]
来自节点的此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
我敢肯定,你可以看到之间的差异var o1 = {p:undefined};
和var o2 = {};
。
在这两种情况下,o.p
将是,undefined
但在第一种情况下,是因为这是值,在第二种情况下是因为没有值。
delete
是一种运算符,可让您从o1
(或具有为其p
属性分配值的另一个对象)获取o2
该方式:delete o1.p;
。
只需通过undefined
为属性分配一个值(在本例中为该值,但可以是其他值)即可完成反向操作o1.p = undefined;
。
所以不,它们不是等效的。
delete o.p;
将
-
p
从对象中删除该属性(如果有一个) -
否则不做任何事情
o.p = undefined;
将
-
p
如果没有属性,则向该对象添加一个属性,并将其值设置为undefined
-
只需更改对象的属性值即可
从性能的角度来看,这delete
是不好的,因为它会修改对象的结构(就像在构造函数中尚未初始化新属性一样,它就像添加新属性一样)。
而将值设置undefined
为也会释放内容,但不强制修改结构。
对象只是树的表示形式,这意味着在内存中,根指向存储该对象的键的各种存储位置。并且该位置指向存储该键的实际值的另一个位置,或者存储子键的位置,或者存储数组值的位置。
当您使用delete从对象中删除任何键时,实际上它会删除该键与其父对象之间的链接,并且键的存储位置及其值将被释放以存储其他信息。
当您尝试通过将undefined设置为键值来删除任何键时,那么您只是在设置其值,而不是删除该键。这意味着密钥存储位置仍与它的父对象和未定义密钥的值链接。
使用undefined而不是delete关键字是一个坏习惯,因为它不会释放该键的内存位置。
即使键不存在,并且您将其设置为undefined,该键也将使用value创建undefined
。
例如
var a = {};
a.d = undefined;
console.log(a); // this will print { d: undefined }
删除不能与继承的属性一起使用,因为该属性不是该子对象的一部分。
通过使用数组而不是对象,我可以展示删除所使用的堆内存少于未定义的堆内存。
例如,此代码不会完成:
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
ary项目(而不是仅将其设置为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
说明了我从未见过有人提及的地方。
文章标签:javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!