是“清楚”的Javascript中的保留字? [英] Is "clear" a reserved word in Javascript?

查看:119
本文介绍了是“清楚”的Javascript中的保留字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很长时间才弄清楚我不应该在Javascript中使用clear()作为函数的名称:

I just spent a long time figuring out that I shouldn't use clear() as the name of a function in Javascript:

<head>
    <script type="text/javascript" src="Array.js"></script>
</head>
<body>
    Hello!!!!<br>
    <button type="button" onClick="clear()" id="ppp">Shoo!</button><br>
    <button type="button" onClick="add()" id="add">Add a few elements</button><br>
    <button type="button" onClick="check()" id="check">Check the array</button><br>
    <p id="results">Results will appear here.</p>
    <script type="text/javascript">
initialize();
    </script>
</body>

这是Array.js:

Here's Array.js:

var results;

function initialize(){
 results = document.getElementById("results");
}

function add() {
results.firstChild.data="add";    
}

function clear() {
results.firstChild.data = "Hello?";
}

function check() {
results.firstChild.data = "check";
}

症状:点击添加和检查按钮可以看到结果我希望,但点击清除按钮不会做任何事情。

Symptoms: Clicking the 'add' and 'check' buttons gives me the result I expect, but clicking the 'clear' button does nothing.

如果我将 clear()重命名为 clearxyz(),它运作正常。

If I rename clear() to clearxyz(), it works fine.

我的问题:


  1. 清除是一个保留字吗?我没有在列表中看到它:
    https://developer.mozilla .org / en / JavaScript / Reference / Reserved_Words

  2. 我是否应该使用调试技巧来计算出这种
    的东西?我花了很长时间(我是菜鸟!)来
    弄清楚功能的名称是我的问题。

非常感谢。
编辑:我正在使用Firefox 6.0,我添加了换行符以显示Array.js的起始位置。

Many thanks. I'm using Firefox 6.0, and I added a line break to show where Array.js starts.

推荐答案

正如其他人所说, clear 保留关键字。似乎被调用的函数是 document.clear [MDN] 。调用

As the others said, clear is not a reserved keyword. It seems that the called function is document.clear [MDN]. Invoking

console.log(clear === document.clear);

在事件处理程序中返回 true

inside the event handler returns true.

DEMO

所以看来, document 在事件处理程序的作用域链中....现在的问题是为什么。

So it seems, document is in the scope chain of the event handler.... the question now is why.

JavaScript:The Definitive Guide 说:

在事件处理程序的HTML属性,文档对象是在窗口对象(...)

In an event handler as HTML attribute, the Document object is in the scope chain before the Window object (...)

由于您的方法是全局的,意味着它是窗口对象的属性,因此在作用域链中找不到它,因为 document.clear 在范围链的早期出现。

As your method is global, meaning it is a property of the window object, it is not found in the scope chain, as document.clear comes earlier in the scope chain.

我还没有发现任何sp为此而实现的。该指南还说不应该依赖于此,所以我认为这不是正式的。

I haven't found any specification for this. The guide also says that one should not rely on that, so I assume this is nothing official.

如果表单中有表单元素,那么即使是相应的 form 元素将在作用域链中(不确定这是否适用于所有浏览器)。这是造成混淆的另一个原因。

If you have form elements inside a form, then even the corresponding form element will be in the scope chain (not sure whether this holds for all browsers though). This is another reason for confusion.

有两种(非独家)方法可以避免这种情况:

There are two (not exclusive) ways to avoid such situations:


  • 不要使用内联事件处理程序。这被认为是不好的做法,因为它混合了逻辑和表示。有其他 方法以附加事件处理程序。

  • Don't use inline event handlers. It is considered bad practice as it is mixing logic and presentation. There are other ways to attach event handlers.

不要污染全局命名空间。 在全局范围内创建一个对象(您确定的名称不会与任何窗口碰撞文档 HTML元素的属性或ID)并将函数指定为此对象的属性。无论何时调用函数,都可以通过此对象引用它。还有其他方式命名您的代码。

Don't pollute the global namespace. Create one object in global scope (with a name you are sure of does not collide with any window or document properties or ids of HTML elements) and assign the functions as properties of this object. Whenever you call a function, you reference it through this object. There also other ways to namespace your code.

这篇关于是“清楚”的Javascript中的保留字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆