“无法创建影响全局属性的重复变量” [英] "can't create duplicate variable that shadows a global property"
本文介绍了“无法创建影响全局属性的重复变量”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Safari 10上获取此错误
Getting this error on Safari 10
发生const给定一个与 id完全相同的名称
任何页面上的元素。
Happens when a const is given a name that's exactly the same as an id
of any element on the page.
const abc = document.querySelector("#abc"); //err
const abz = document.querySelector("#abc"); //no err
var abc = document.querySelector("#abc"); //no err
有没有理由?
我知道你可以通过window.abc访问元素,这是为什么的一部分?
Is there a reason for this?
I know you can access the elements via window.abc, is that part of the why?
测试:
<!doctype html>
<html><body>
<div id="alpha"></div>
<script>
const alpha = document.querySelector('#alpha');
console.log(alpha);
</script>
</body></html>
推荐答案
这显然是一个问题,Safari已经声明一个let / const变量与所选的id属性共享相同的名称。我有一些变量用const声明,并且得到相同的错误。改变了让,仍然得到它。更改为var,它工作。
This is apparently a problem Safari has with declaring a let/const variable that shares the same name as a selected id attribute. I had some variables declared with const as you did and got the same error. Changed to let and still got it. Changed to var and it worked.
这篇关于“无法创建影响全局属性的重复变量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文