Today code, I encountered the case to delete a property in the object. At first glance, it seems simple but contains unexpected knowledge. ?
As you know, there are 2 ways to “remove” a property from an object in JavaScript:
- Use
delete
operator12<span class="token keyword">delete</span> foo <span class="token punctuation">.</span> bar - Assign the property to be deleted with
undefined
12foo <span class="token punctuation">.</span> bar <span class="token operator">=</span> undefined
After a while Google Attorney General, I can summarize the differences between the two ways.
1. Assigning a property with undefined
does not completely delete that property
The first thing you should know is that assigning foo.bar
with undefined
simply gives the property a value of undefined
. This means that when you use the hasOwnProperty()
function or loop the object in a for in
loop, the property still exists.
1 2 3 4 5 6 7 8 | foo <span class="token punctuation">.</span> <span class="token function">hasOwnProperty</span> <span class="token punctuation">(</span> <span class="token string">'bar'</span> <span class="token punctuation">)</span> <span class="token comment">// returns true</span> <span class="token keyword">for</span> <span class="token punctuation">(</span> <span class="token keyword">const</span> key <span class="token keyword">in</span> foo <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> key <span class="token operator">===</span> <span class="token string">'bar'</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">'I am still alive'</span> <span class="token punctuation">)</span> <span class="token comment">// log `I am still a live`</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
If you use the delete
operator, the property will be completely deleted, and the results in the above code will be reversed.
The reason is because the delete
operator changes the hidden class , which is equivalent to the fact that the property bar
has never been declared. This does not occur when assigning foo.bar
with undefined
, as this only reassigns the value of the property bar
. So if you want to check if an object exists for certain properties, use the hasOwnProperty()
function hasOwnProperty()
than a simple check:
1 2 | <span class="token keyword">if</span> <span class="token punctuation">(</span> foo <span class="token punctuation">.</span> bar <span class="token operator">===</span> undefined <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> |
Try to understand the difference between undefined and not defined in JavaScript again, it’s quite interesting! ?
2. What if the object has a prototype chain?
I have an example as follows:
1 2 3 4 5 6 7 8 9 10 | <span class="token keyword">const</span> origin <span class="token operator">=</span> <span class="token punctuation">{</span> x <span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> extended <span class="token operator">=</span> Object <span class="token punctuation">.</span> <span class="token function">create</span> <span class="token punctuation">(</span> origin <span class="token punctuation">)</span> extended <span class="token punctuation">.</span> x <span class="token operator">=</span> <span class="token number">2</span> <span class="token keyword">delete</span> extended <span class="token punctuation">.</span> x console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> extended <span class="token punctuation">.</span> x <span class="token punctuation">)</span> <span class="token comment">// ???</span> |
Have you guessed what the above code will log out?
If you think the result is undefined
then I am sorry, today you clicked =))
Here we have an extended
object whose prototype is object origin
. After calling delete
, each time revisit extended.x
JavaScript engine will look up from extended
chain prototype and find origin
property x
. The result returns the console as 1
.
OK, so, what happens when I change the last two lines of the above code to the following:
1 2 3 | extended <span class="token punctuation">.</span> x <span class="token operator">=</span> undefined console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> extended <span class="token punctuation">.</span> x <span class="token punctuation">)</span> <span class="token comment">// ???</span> |
Now the log output will be undefined
. Because property x
still exists in extended
with the value equal to undefined
, there will be no looking up from the prototype chain.
The catch is that when you “remove” a property from an object, if you are unsure whether the object has a prototype chain and if the object inherits the prototype that contains the property to be deleted, you should assign it with undefined
instead. Use delete
operator.
3. “I have a Linux machine with 0.8GHz CPU and 256MB RAM, I care about performance!”
As mentioned in part 1, delete
a property will change the hidden class, meaning that the performance is affected. You should avoid using delete
operator too much, especially in the loop. However, this does not mean that you should never use this operator. Optimizing the code at the JavaScript engine level will not be worth a tradeoff if it makes your code read as economically.
This article has helped you understand the difference between 2 ways to remove property from object in JavaScript. Hope it helps you to level up to go out with colleagues. ? Thank you for reading my article.
Reference from: