admin管理员组文章数量:1323323
If I open my JavaScript console (F12 on Chrome) and define a variable var x = {};
and then type x
in the console it shows me an empty object like this: {}
. So far so good.
If try to see x.a
the console will give me undefined
, and if I explicitly test x.a === undefined
it gives true
. So far so good.
Everything is telling me that the value of x.a
is undefined
.
So, since its value is undefined, I now execute a mand that I was expecting to do absolutely nothing: x.a = undefined;
. I thought this shouldn't change anything.
But now if I put x
in the console, it shows {a: undefined}
instead of {}
. This is an evidence that something changed...
1. What is happening here?
2. Is there any situation in which this would make a difference?
3. If yes, how can I make that object go back to its initial state?
Note: of course, for question 3, re-assigning to a new {}
is obviously not an option, since it would no longer ===
to the previous value.
If I open my JavaScript console (F12 on Chrome) and define a variable var x = {};
and then type x
in the console it shows me an empty object like this: {}
. So far so good.
If try to see x.a
the console will give me undefined
, and if I explicitly test x.a === undefined
it gives true
. So far so good.
Everything is telling me that the value of x.a
is undefined
.
So, since its value is undefined, I now execute a mand that I was expecting to do absolutely nothing: x.a = undefined;
. I thought this shouldn't change anything.
But now if I put x
in the console, it shows {a: undefined}
instead of {}
. This is an evidence that something changed...
1. What is happening here?
2. Is there any situation in which this would make a difference?
3. If yes, how can I make that object go back to its initial state?
Note: of course, for question 3, re-assigning to a new {}
is obviously not an option, since it would no longer ===
to the previous value.
-
You got good answers down below, but I'm going to address that last note you added: remember that object literals will never be equal to other object literals, except the ones that reference that same literal object. For example, doing
{} === {}
returnsfalse
, but doingvar a = b = {}; a === b;
turns out to betrue
– GMaiolo Commented Nov 1, 2017 at 23:12 - 1 @GMaiolo, thanks, I know, that's exactly why I said that re-assigning to a new {} is not an option. – Pedro A Commented Nov 1, 2017 at 23:14
- 1 I was adding the clarification to your note just in case someone that does not know that happens to read this question. – GMaiolo Commented Nov 1, 2017 at 23:19
3 Answers
Reset to default 51. What is happening here?
At first you are accessing a non-existing property. That will always return undefined
as defined in the language specification.
But a property can also exist and have the value undefined
, which is what you are getting when doing
x.a = undefined;
This creates the property a
on x
and assigns the value undefined
to it.
2. Is there any situation in which this would make a difference?
Testing the existence of the property would result in false
in the first case and true
in the second case.
var x = {};
console.log('a exists', 'a' in x);
x.a = undefined;
console.log('a exists', 'a' in x);
The value of the property is irrelevant when testing for existence.
Similarly, getting all properties of an object would yield different results:
var x = {};
console.log(Object.keys(x));
x.a = undefined;
console.log(Object.keys(x));
3. If yes, how can I make that object go back to its initial state?
You can delete properties using the delete
operator:
delete x.a;
JavaScript treats the setting of a property to any value as a meaningful thing. The in
operator illustrates:
var x = {};
console.log("a" in x); // false
x.a = undefined;
console.log("a" in x); // true
The in
operator is the solid way to test whether a particular key is present in an object regardless of value. There's also the Object.hasOwnProperty()
function if it matters that the property be directly present on the object.
If you want to get rid of a property pletely, you can use delete
:
delete x.a;
console.log("a" in x); // false
The undefined
"value" (is it really a value?) is essentially a wart on the design of JavaScript; it makes it such that the language has effectively two null
values. I understand why it's there, but that doesn't make the situation any easier to deal with.
You can remove the key like so:
delete x.a;
本文标签: Setting an undefined key of an object to undefined in JavaScriptStack Overflow
版权声明:本文标题:Setting an undefined key of an object to undefined in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742142435a2422640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论