简体   繁体   English

为什么Firebug允许删除在其控制台中声明的全局变量?

[英]Why does Firebug allow global variables declared in its console to be deleted?

I read in a book(JavaScript - the definitive Guide 6th Edition) that a global variable declared using var keyword cannot be delete using delete keyword. 我在一本书(JavaScript - 权威指南第6版)中读到,使用var关键字声明的全局变量不能使用delete关键字delete But I am able to do it running it in Firebug Console. 但我能够在Firebug Console中运行它。

var a = 1;
delete a;// should return false but returns true in firebug.

I am not able to figure it out, why this is happening? 我无法弄清楚,为什么会发生这种情况?

Edit 编辑

My question is not about how to unset a global variable, it is about if you declare a global variable as I did using var keyword, it creates a nonconfigurable property of the global object which cannot be deleted using delete keyword. 我的问题不是关于如何取消设置全局变量,而是如果你使用var关键字声明一个全局变量,它会创建一个全局对象的不可配置属性,不能使用delete关键字delete But I am able to do it in Firebug Console, which should not happen. 但我能够在Firebug控制台中完成,这不应该发生。

Here is detailed answer 这是详细的答案

It will work, but technically it ought to be 它会起作用,但技术上应该是

delete window.some_var; 

delete is supposed to be a no-op when the target isn't an object property. 当目标不是对象属性时,delete应该是无操作。 eg, 例如,

(function() {
   var foo = 123;
   delete foo; // wont do anything, foo is still 123
   var bar = { foo: 123 };
   delete bar.foo; // foo is gone
}());

But since global variables are actually members of the window object, it works. 但由于全局变量实际上是窗口对象的成员,因此它可以工作。

When prototype chains are involved, using delete gets more complex because it only removes the property from the target object, and not the prototype. 当涉及原型链时,使用delete会变得更复杂,因为它只从目标对象中移除属性,而不是原型。 eg, 例如,

function Foo() {}
Foo.prototype = { bar: 123 };
var foo = new Foo();
// foo.bar is 123
foo.bar = 456;
// foo.bar is now 456
delete foo.bar;
// foo.bar is 123 again.

So be careful. 所以要小心。

EDIT: My answer is somewhat inaccurate (see "Misconceptions" at the end). 编辑:我的回答有些不准确 (最后请参阅“误解”)。 The link explains all the gory details, but the summary is that there can be big differences between browsers and depending on the object you are deleting from. 该链接解释了所有血腥细节,但摘要是浏览器之间可能存在很大差异,具体取决于您要删除的对象。 delete object.someProp should generally be safe as long as object !== window . 只要object !== window delete object.someProp通常应该是安全的。 I still wouldn't use it to delete variables declared with var although you can under the right circumstances. 我仍然不会用它来删除用var声明的变量,尽管你可以在适当的情况下使用它。

Code in the Firebug console is executed with eval . Firebug控制台中的代码使用eval执行。 And variable bindings created in eval code are mutable (this depends on the variable not already having been declared) and can be deleted. 并且在eval代码中创建的变量绑定是可变的(这取决于尚未声明的变量)并且可以删除。

See the ECMAScript Language Specification , section 10.5 item 2: 请参阅ECMAScript语言规范 ,第10.5节第2项:

If code is eval code, then let configurableBindings be true else let configurableBindings be false . 如果代码是eval代码,那么让configurableBindingstrue,否则让configurableBindingsfalse

and item 8.ci: 和项目8.ci:

Call env ’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments. 调用env的CreateMutableBinding具体方法,将dnconfigurableBindings作为参数传递。

along with Table 17 in section 10.2.1: 以及10.2.1节中的表17:

CreateMutableBinding(N, D) CreateMutableBinding(N,D)
Create a new mutable binding in an environment record. 在环境记录中创建新的可变绑定。 The String value N is the text of the bound name. 字符串值N是绑定名称的文本。 If the optional Boolean argument D is true the binding is may be subsequently deleted. 如果可选的布尔参数D ,则可以随后删除绑定。

As @NagaJolokia points out, delete is behaving differently in the Firebug console because the console is using eval() to execute the code. 正如@NagaJolokia指出的那样,在Firebug控制台中delete行为有所不同,因为控制台正在使用eval()来执行代码。

You can see the same effect in action if you test a delete both in normal code and under eval() . 如果在普通代码和eval()下测试delete则可以看到相同的效果。 Save this page and load it in any browser with the developer console open: 保存此页面并在打开开发人员控制台的任何浏览器中加载它:

<!DOCTYPE html>
<html>
<head>
    <title>Eval/Delete Test</title>
    <script>
        console.log( 'Normal code, a = 1' );
        var a = 1;
        console.log( 'a is', typeof a, a );
        console.log( 'window.a is', typeof window.a, window.a );
        console.log( 'delete a OK?', delete a );
        console.log( 'delete window.a OK?', delete window.a );
        console.log( 'a is now', typeof a, window.a );
        console.log( 'window.a is now', typeof window.a, window.a );
        console.log( ' ' );
        console.log( 'Eval code, b = 1' );
        eval( 'var b = 1;' );
        console.log( 'delete b OK?', delete b );
        console.log( 'b is now', typeof b, window.b );
    </script>
</head>
<body>
</body>
</html>

The code will log: 代码将记录:

Normal code, a = 1
a is number 1
window.a is number 1
delete a OK? false
delete window.a OK? false
a is now number 1
window.a is now number 1

Eval code, b = 1
delete b OK? true
b is now undefined undefined

I also made a fiddle with the same code ready to run. 我还做了一个小提琴使用相同的代码可以运行。 It produces the same output as above. 它产生与上面相同的输出。 The fiddle includes Firebug Lite , so you don't need to open the developer console. 小提琴包括Firebug Lite ,因此您无需打开开发者控制台。

For a more thorough explanation, see NagaJolokia's answer and the Firebug confusion section of this article . 有关更全面的解释,请参阅NagaJolokia的回答和本文的Firebug混淆部分

检查你的拼写

truevar != trueval

Seems to be you did spelling mistake 好像你拼错了

var trueval = 1;
delete truevar;//spelling mistake here

You can't delete the global variable but you do like this 您无法删除全局变量,但您确实喜欢这样

function Foo() {}

Foo.prototype = { bar: 123 };

var foo = new Foo();

// foo.bar is 123

foo.bar = 456;

// foo.bar is now 456

delete foo.bar;
// foo.bar is 123 again.

For more reference How to unset a JavaScript variable? 有关更多参考如何取消设置JavaScript变量?

http://perfectionkills.com/understanding-delete/ http://perfectionkills.com/understanding-delete/

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

相关问题 为什么不能删除全局变量? - why can't global variables be deleted? 将对象传递给指令不允许全局变量 - Passing object to directive does not allow global variables Firefox addon global object vs. firebug console global object - Firefox addon global object vs. firebug console global object 为什么我的事件处理程序会导致“不是函数”错误,但是可以从Firebug控制台运行? - Why does my event handler cause an “is not a function” error, but works from the Firebug console? Firebug 和 google 控制台都不允许更改通过 pdf.js 呈现的内容,为什么? 如何? - Firebug and google console both does not let change content rendered through pdf.js why ? and how? 当我输入json对象时,firebug控制台为什么总是说“ undefined”? - Why does the firebug console keep saying “undefined” when I enter a json object? 为什么我的Ajax请求显示错误,然后立即从Firebug控制台中消失? - Why does my ajax request show error then disappear immediately from firebug console? 为什么 Firefox DevTools 和 Firebug 控制台中有很多警告? - Why are there lots of warnings in the Firefox DevTools and Firebug console? 为什么Angular无法登录Firebug控制台 - Why won't Angular log to Firebug console console.log在Liferay中的Firebug中什么都不做 - console.log does nothing in Firebug in Liferay
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM