是“删除” Google Chrome中的保留关键字? [英] Is "remove" a reserved keyword in Google Chrome?

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

问题描述

我有一个有趣的问题,我想我已经明白了它的根源,但我想确定。我有一个链接调用一个名为remove()的函数。除Chrome之外的所有浏览器都不存在该功能的问题。但是,点击链接在Chrome中消失了,即使我在下面的示例中简化了该功能。我看到这个问题:不能使用下载作为JavaScript中的函数名称。然而,在链接中,我没有看到关于删除作为保留关键字的任何信息。我的问题是,我对这是一个关键字是正确的吗?如果是这样,有什么地方我可以找到一个谷歌关键字列表?

 < a href =javascript:void(0)我已搜索并未发现这是其他地方的问题。 )onclick =remove()>删除< / a> 

Javascript:

  function remove(){
alert(Hi);
}


解决方案

Chrome中的元素具有 .remove()方法,它允许自行删除元素,而不必从父级执行。



麻烦的是,当使用属性处理程序时,您会得到一个不同的作用域链。该范围链包含元素本身以及文档。这意味着元素和文档的所有属性都显示为变量。



因为您将函数命名为 remove(),并且因为它是一个全局函数/变量,所以它被 .remove 属性遮蔽。现在变量)在元素本身。这可以通过警报来看到。如果您将处理程序更改为:

  onclick =alert(remove)



...您会得到如下所示的内容:

  function remove(){[native code]} 

所以并不是它保留,而是它被用作最终影响全局的属性。






要修复它,可以直接使用全局:

  onclick =window.remove()

或更改函数名称。


I have an interesting problem, and I think I got to the root of it, but I wanted to be sure. I have a link that calls a function called remove(). All browsers except Chrome had no issues with the function. However, the link that is clicked disappeared in Chrome, even when I simplified the function as in the example below. I have seen this question: Can't use "download" as a function name in javascript. In the links, however, I did not see anything about "remove" as a reserved keyword. My question is this, I am correct about this being a keyword? If so, is there anywhere I can find a list of Google keywords? I have searched and have not found this to be a problem anywhere else.

<a href="javascript:void(0)" onclick="remove()">Remove</a>

Javascript:

function remove(){
 alert("Hi");
}

解决方案

Elements in Chrome have a .remove() method which allows for self-removal of an element instead of having to do it from the parent.

The trouble is that when using attribute handlers, you get a different scope chain. That scope chain includes the element itself, as well as the document. This means that all properties of the element and document show up as variables.

Because you named your function remove(), and because it's a global function/variable, it is being shadowed by the .remove property (now variable) on the element itself. This can be seen with an alert. If you change your handler to:

onclick="alert(remove)"

...you'll get something like:

function remove() { [native code] }

So it's not that it's reserved, but rather that it's used as a property which ends up shadowing the global.


To fix it, either use the global directly:

onclick="window.remove()"

Or change the function name.

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

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