为什么这个js代码有效?

use*_*729 1 javascript

if (!wysiwyg_toolbarButtons) {
    var wysiwyg_toolbarButtons = new Array(
        //command, display name, value, title, prompt/function, default text
        ["bold", "Strong", WYSIWYG_VALUE_NONE, "Give text strength"],
        ["italic", "Emphasis", WYSIWYG_VALUE_NONE, "Give text emphasis"],
        ["createlink", "Link", WYSIWYG_VALUE_PROMPT, "Create a hyperlink", "Enter the URL:", "http://"],
        ["unlink", "Unlink", WYSIWYG_VALUE_NONE, "Remove hyperlink"],
        ["insertimage", "Image", WYSIWYG_VALUE_PROMPT, "Insert an image", "Enter the URL of the image:", "http://"],
        ["inserthorizontalrule", "Rule", WYSIWYG_VALUE_NONE, "Insert horizontal rule"],
        ["div"], // place a toolbar divider
        ["formatblock", "Headling 1", "<H1>", "Make top level heading"],
        ["formatblock", "Headling 2", "<H2>", "Make 2nd level heading"],
        ["formatblock", "Headling 3", "<H3>", "Make 3rd level heading"],
        ["formatblock", "Paragraph", "<P>", "Make a paragraph"],
        ["formatblock", "Monospace", "<PRE>", "Make paragraph monospaced text"],
        ["insertunorderedlist", "List", null, "Make an unordered list"],
        ["insertorderedlist", "Ordered List", null, "Make an ordered list"],
        ["div"], // place a toolbar divider
        ["toggleview", "Source", "Compose", "Switch views"]
    );
}
Run Code Online (Sandbox Code Playgroud)

它来自这个文件,这里有一个演示

我的问题是:为什么不报告"ReferenceError:wysiwyg_toolbarButtons未定义"?

Fru*_*nsi 5

Web浏览器中的JavaScript搜索窗口对象中的属性.访问未知属性不会引发错误,因此实际上它被评估为如下所示:

if( !window.wysiwyg_toolbarButtons ) { }
Run Code Online (Sandbox Code Playgroud)

尝试if( !wtf ) { alert('error'); }if( !window.wtf ) { alert('no error'); }在Firebug控制台中.

编辑

目前,firebug中的控制台使用了代码with( window ) { ..console..code.. }.但是"with"语句很棘手,例如:

>>> alert(location);
= eval( "with( window ) { alert(location); }" );
OK, "location" attribute found in window

>>> alert(wtf);
= eval( "with( window ) { alert(wtf); }" );
ERROR, "wtf" not found in window, and not in global scope, throws ReferenceError
Run Code Online (Sandbox Code Playgroud)

浏览器中的隐式窗口对象的行为与"with"语句使用的行为不同.

  • "bug"是因为Firebug执行包含在_with_语句中的输入:`with(window} {..console..code ..}`.另请参阅http://stackoverflow.com/questions/61552/are-there -legitimate用途换JavaScript的 - 与语句 (2认同)