gpt4 book ai didi

javascript - 为什么这个js代码有效?

转载 作者:行者123 更新时间:2023-11-30 09:09:11 25 4
gpt4 key购买 nike

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"]
);
}

来自this file ,带演示here

我的问题是:为什么不报“ReferenceError: wysiwyg_toolbarButtons is not defined”?

最佳答案

Web 浏览器中的 JavaScript 搜索窗口对象 中的属性。访问未知属性不会抛出错误,因此实际上它的计算方式如下:

if( !window.wysiwyg_toolbarButtons ) { }

尝试 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

浏览器中的隐式窗口对象的行为不同于它与“with”语句一起使用时的行为。

关于javascript - 为什么这个js代码有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1885452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com