正如@NagaJolokia 指出的那样,delete
在 Firebug 控制台中的行为不同,因为控制台eval()
用于执行代码。
如果您delete
在普通代码和eval()
. 保存此页面并在打开开发者控制台的任何浏览器中加载它:
<!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>
代码将记录:
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
我还用准备运行的相同代码做了一个小提琴。它产生与上面相同的输出。小提琴包括Firebug Lite,因此您无需打开开发者控制台。
有关更详尽的解释,请参阅NagaJolokia 的回答和本文的Firebug 混淆部分。