Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 相同的变量在不同的'&书信电报;脚本>';没有冲突的标签?_Javascript_Variables_Namespaces_Javascript Namespaces - Fatal编程技术网

Javascript 相同的变量在不同的'&书信电报;脚本>';没有冲突的标签?

Javascript 相同的变量在不同的'&书信电报;脚本>';没有冲突的标签?,javascript,variables,namespaces,javascript-namespaces,Javascript,Variables,Namespaces,Javascript Namespaces,我希望在不同的代码块中使用相同的变量,而不会发生冲突。我的代码大致如下所示: <html> <head> <script type="text/javascript"> ad = document.getElementById('sidebar1-ad'); if (ad.getBoundingClientRect().width) { adWidth = ad.getBoundingClien

我希望在不同的
代码块中使用相同的变量,而不会发生冲突。我的代码大致如下所示:

<html>
<head>
    <script type="text/javascript">
        ad = document.getElementById('sidebar1-ad');

        if (ad.getBoundingClientRect().width) {
            adWidth = ad.getBoundingClientRect().width;
        } else {
            adWidth = ad.offsetWidth;
        }

        ...
    </script>

    <script type="text/javascript">
        ad = document.getElementById('article-footer-ad');

        if (ad.getBoundingClientRect().width) {
            adWidth = ad.getBoundingClientRect().width;
        } else {
            adWidth = ad.offsetWidth;
        }

        ...
    </script>
</head>

<body> ... </body>
</html>

ad=document.getElementById('sidebar1-ad');
if(ad.getBoundingClientRect().width){
adWidth=ad.getBoundingClientRect().width;
}否则{
adWidth=ad.offsetWidth;
}
...
ad=document.getElementById('article-footer-ad');
if(ad.getBoundingClientRect().width){
adWidth=ad.getBoundingClientRect().width;
}否则{
adWidth=ad.offsetWidth;
}
...
... 
问题是,第二个代码块中的变量
ad
adWidth
似乎优先


有没有一种方法可以在一个页面的不同
标记中使用相同的变量名,而不让任何一个覆盖另一个?如果是,怎么做?

如果您可以更改脚本主体,只需将代码包装到匿名函数中并立即调用它即可。此外,您需要使用
var
关键字来限制变量范围

<script>
  (function() {

      var ad = document.getElementById('article-footer-ad');

      // your code here ...

  })(); // <-- this is an immediate call
</script>

(功能(){
var ad=document.getElementById('article-footer-ad');
//你的代码在这里。。。
})(); // <代码>
(功能()
{
var ad=document.getElementById('sidebar1-ad');
if(ad.getBoundingClientRect().width){
var adWidth=ad.getBoundingClientRect().width;
}否则{
var adWidth=ad.offsetWidth;
}
...
})();
(功能()
{
var ad=document.getElementById('article-footer-ad');
if(ad.getBoundingClientRect().width){
var adWidth=ad.getBoundingClientRect().width;
}否则{
var adWidth=ad.offsetWidth;
}
...
})();

。。。

然后需要命名名称空间,或者在函数中隔离它们,并使用car关键字。嗯。。。谢谢,电话。@DaveNewton Heh,我想知道…:谢谢你的建议。好吧,但我该怎么称呼它呢?是的,我注意到了。非常感谢您抽出时间!顺便问一下,
adWidth
变量不也需要
var
<script>
    // Common function

    function renderAd(adId) {

        var ad = document.getElementById(adId);

    }
</script>
...
<script>
    renderAd('sidebar1-ad');
</script>
...
<script>
    renderAd('article-footer-ad');
</script>
<html>
<head>
<script type="text/javascript">
(function()
{
    var ad = document.getElementById('sidebar1-ad');

    if (ad.getBoundingClientRect().width) {
        var adWidth = ad.getBoundingClientRect().width;
    } else {
        var adWidth = ad.offsetWidth;
    }

    ...
})();
</script>

<script type="text/javascript">
(function()
{
    var ad = document.getElementById('article-footer-ad');

    if (ad.getBoundingClientRect().width) {
        var adWidth = ad.getBoundingClientRect().width;
    } else {
        var adWidth = ad.offsetWidth;
    }

    ...
})();
</script>