不能使用“下载”作为JavaScript中的函数名称 [英] Can't use "download" as a function name in javascript

查看:93
本文介绍了不能使用“下载”作为JavaScript中的函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个小片段中遇到了问题:

 < script> 
函数下载(){
alert('Hi');
}
< / script>
< a href =#onClick =javascript:download();>测试< / a>

一旦我点击Chrome 14.0中的链接,就会得到一个

  Uncaught TypeError:字符串不是函数



在Firefox和IE中它工作得很好。我通过重命名这个函数解决了这个问题,但我仍然很好奇Chrome的下载功能。这不是一个保留的关键字,据我所知,它可能是什么?

< a> ; 元素在HTML5中具有下载属性,如 here ,默认值为

这意味着在 download === this.download > onclick 处理程序(这是 onevent 属性中的元素),因此<元素的code> download 属性优于窗口的下载属性>。



此小提琴列出所有字符串默认存在的属性。你可以看到 download 是一个属性,就像 innerHTML ,它也会失败,当用作函数时(即试图引用 window.innerHTML ,而是执行 elem.innerHTML())。

正如在评论中所说的,使用 window 不会混淆属性/属性变量的计算结果。






这个范围行为实际上并不是由于 this 值而是由于正在构建的特定范围链。



根据 HTML5规范


词法环境范围

范围 NewObjectEnvironment(元素的Document,全局env如果元素有一个表单所有者,让 Scope 为结果 NewObjectEnvironment(元素的表单所有者,范围)


$ b code>是的结果NewObjectEnvironment(元素的对象,作用域)




。即,范围链是窗口 - > 文档 - > 元素(增加优势)。这意味着 download 的计算结果为 element.download 而不是 window.download 。由此可以得出结论: getElementById 会冒泡到 document.getElementById (给定 elem.getElementById 不存在)。



我设置了一个系统示例,以便您可以看到变量如何在作用域链上冒出来:

  window.a = 1; 
document.a = 2;
elem.a = 3;

window.b = 4;
document.b = 5;

window.c = 6;

然后,< a ... onclick =console.log a,b,c)> logs 3 5 6 点击。


I've had a problem with this little snippet:

<script>
function download() {
    alert('Hi');
}
</script>
<a href="#" onClick="javascript:download();">Test</a>

Once I click on the link in Chrome 14.0, I get a

Uncaught TypeError: string is not a function

in Firefox and IE it works just fine. I solved the problem by renaming the function but I'm still curious what's with the "download" thing in Chrome. It's not a reserved keyword as far as I know so what might it be?

解决方案

<a> elements have a download attribute in HTML5 as explained here, with a default value of "" (an empty string).

This means that download === this.download in the onclick handler (this is the element in onevent attributes), and therefore the download attribute of the element is superior to the download property of window.

This fiddle lists all string attributes that are present by default. You can see download is an attribute just like innerHTML, which also fails with the exact same reason when used as a function (i.e. trying to refer to window.innerHTML, but instead executing elem.innerHTML()).

As said in the comments, using window makes for no confusion as to what property/attribute variables will evaluate to.


This scope behaviour does actually not seem to due to the this value but rather a specific "scope chain" that is being constructed.

As per the HTML5 specification:

Lexical Environment Scope

Let Scope be the result of NewObjectEnvironment(the element's Document, the global environment).

If the element has a form owner, let Scope be the result of NewObjectEnvironment(the element's form owner, Scope).

Let Scope be the result of NewObjectEnvironment(the element's object, Scope).

I.e. what is happening is the scope chain is window -> document -> element (increasing superiority). This means that download evaluates to element.download and not window.download. What also can be concluded from this is that getElementById will bubble up to document.getElementById (given elem.getElementById does not exist).

I set up a systematic example so that you can see how variables bubble up the scope chain:

window.a   = 1;
document.a = 2;
elem.a     = 3;

window.b   = 4;
document.b = 5;

window.c   = 6;

Then, <a ... onclick="console.log(a, b, c)"> logs 3, 5, 6 when clicked.

这篇关于不能使用“下载”作为JavaScript中的函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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