I was searching for a suitable explanation for this on SO, but couldn't find the one which answers my question.
I read that in JavaScript, Objects couldn't be deleted. So to find out, I was playing around in my browser's console. I created an object like this:
var a = {x:10};
Then I did delete a.x
which returned true
.(No surprises here)
Then went on to delete the object like this: delete a
.
But what stumped me was while Google Chrome
returned false
, Firefox
returned true
How is it possible that an Object is "deleted" in one browser and not in another? Is there something I am missing here or Is it browser implementation that causes this?
In FF v27:
In Google Chrome v33 .
This is due to differences in the internal method of running console code in Firefox and Chrome.
In Firebug, console code is evaluated using a form of eval
for extension code. However, in Chrome the code in the console is evaluated using internal methods1 which simulate actual code running, not directly using JavaScript's eval
function.
The [[Configurable]]
internal property descriptor attribute determines whether an attempt to delete the variable/property will succeed. If it's false, it will not delete the property and the delete
operator will return false.
Any variables defined in eval code have [[Configurable]]
set to true
. However, if you define a variable outside code passed to eval
, that attribute will be set to false
.
This difference in behaviour between eval
and other types of executable code is specified in the ECMAScript standard under section 10.5:
2. If code is eval code, then let configurableBindings be true else let configurableBindings be false.
1: This code is only the frontend code, not the actual internals, which goes down many levels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With