Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript delete objects behaves differently in different browsers

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: Firefox Console

In Google Chrome v33 Google Chrome Console.

like image 494
Sandeep Nayak Avatar asked Oct 01 '22 11:10

Sandeep Nayak


1 Answers

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.

like image 71
Qantas 94 Heavy Avatar answered Oct 13 '22 11:10

Qantas 94 Heavy