简体   繁体   English

JavaScript删除对象在不同的​​浏览器中的行为有所不同

[英]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. 我正在SO上为此寻找合适的解释,但找不到能回答我的问题的解释。

I read that in JavaScript, Objects couldn't be deleted. 我在JavaScript中读到,无法删除对象。 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 ax which returned true .(No surprises here) 然后我确实delete ax返回true delete ax

Then went on to delete the object like this: delete a . 然后继续删除这样的对象: delete a

But what stumped me was while Google Chrome returned false , Firefox returned true 但是令我感到困扰的是,当Google Chrome返回falseFirefox返回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: 在FF v27中: Firefox控制台

In Google Chrome v33 在Google Chrome v33中 Google Chrome浏览器控制台 .

This is due to differences in the internal method of running console code in Firefox and Chrome. 这是由于在Firefox和Chrome中运行控制台代码的内部方法不同。

In Firebug, console code is evaluated using a form of eval for extension code . 在Firebug中,控制台代码使用eval形式的扩展代码进行eval However, in Chrome the code in the console is evaluated using internal methods 1 which simulate actual code running, not directly using JavaScript's eval function. 但是,在Chrome中,控制台中的代码是使用内部方法 1 评估的 ,该方法模拟实际的代码运行,而不是直接使用JavaScript的eval函数。

The [[Configurable]] internal property descriptor attribute determines whether an attempt to delete the variable/property will succeed. [[Configurable]]内部属性描述符属性确定删除变量/属性的尝试是否将成功。 If it's false, it will not delete the property and the delete operator will return false. 如果为false,则不会删除该属性,并且delete运算符将返回false。

Any variables defined in eval code have [[Configurable]] set to true . 评估代码中定义的任何变量都将[[Configurable]]设置为true However, if you define a variable outside code passed to eval , that attribute will be set to false . 但是,如果在传递给eval代码外部定义变量,则该属性将设置为false

This difference in behaviour between eval and other types of executable code is specified in the ECMAScript standard under section 10.5: eval和其他类型的可执行代码之间的行为差​​异在ECMAScript标准的10.5节中指定:

2. If code is eval code, then let configurableBindings be true else let configurableBindings be false . 2.如果代码是评估代码,则使configurableBindingstrue,否则使configurableBindingsfalse

1: This code is only the frontend code, not the actual internals, which goes down many levels. 1:此代码仅是前端代码,而不是实际的内部代码,它分为许多层次。

I found this : it seems is a Firebug issue: When you use the built-in developer tool in Chrome, it is directly using the JS engine. 我发现了这个问题:似乎是Firebug的问题:当您在Chrome中使用内置的开发人员工具时,它是直接使用JS引擎。 Firebug is a plugin created in JS, so the only way to execute something is using "eval", which seems it's not adding the "don't delete" property to the object. Firebug是用JS创建的插件,因此执行某项操作的唯一方法是使用“ eval”,这似乎并未在对象中添加“请勿删除”属性。

delete removes objects, but not variables. delete删除对象,但不删除变量。

I'll add some point to @Qantas's answer: 我会在@Qantas的答案中添加一些观点:

According to MDN delete operator reference : 根据MDN删除操作员参考

Returns Throws in strict mode if the property is an own non-configurable property (returns false in non-strict). 如果该属性是自己的不可配置属性,则以严格模式返回Throw(在非严格条件下返回false)。 Returns true in all other cases. 在所有其他情况下返回true。

That means, a non-configurable variable cannot be deleted through a delete operator. 这意味着不可配置的变量不能通过删除操作符删除。 However, if a variable is configurable, as in @Qantas's answer, is defined in a eval() , then it can be deleted using the delete operator. 但是,如果在eval()定义了一个变量,如@Qantas的答案所示,则可以使用delete运算符将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM