問(wèn)題描述
有了這個(gè)頁(yè)面:
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
var foo = 2;
delete foo;
</script>
</head>
<body></body>
</html>
Firebug 控制臺(tái)給出:
Firebug console gives:
applying the 'delete' operator to an unqualified name is deprecated
>>> foo
ReferenceError: foo is not defined
foo
但是這樣就成功了:
>>> var bar = 2;
undefined
>>> delete bar;
true
即使您注釋掉 delete foo;
以便腳本不會(huì)中斷,刪除 bar
仍然是成功的,盡管它是全局對(duì)象的屬性"因?yàn)樗峭ㄟ^(guò)變量聲明創(chuàng)建的,所以 DontDelete 屬性":
Even if you comment out delete foo;
so that the script does not break, deleting bar
is still successful despite the fact it "is a property of a Global object as it is created via variable declaration and so has DontDelete attribute":
>>> foo
2
>>> delete foo
false
>>> var bar = 2;
undefined
>>> delete bar
true
是否可以在 FireBug 和/或 Chrome 的控制臺(tái)中啟用嚴(yán)格模式"?
Is it possible to enable "strict mode" in FireBug and or Chrome's console?
推薦答案
firebug 控制臺(tái)通過(guò)將所有代碼包裝在eval"調(diào)用中來(lái)工作,因此腳本中的第一條語(yǔ)句不再是use strict" - 因此它是禁用.您可以嘗試將代碼包裝在一個(gè)函數(shù)中以對(duì)該特定函數(shù)強(qiáng)制使用嚴(yán)格",但我所知道的最佳解決方案是跳過(guò)控制臺(tái)并直接在頁(yè)面本身中進(jìn)行測(cè)試.
The firebug console works by wrapping all the code in an "eval" call so the first statement in your script is no longer "use strict" - hence it is disabled. You could try wrapping your code in a function to enforce "use strict" for that particular function but the best solution I know of is to skip the console and test straight in the page itself.
這篇關(guān)于可以啟用“嚴(yán)格模式";在 FireBug 和 Chrome 的控制臺(tái)中?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!