Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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_Jquery_Variables - Fatal编程技术网

Javascript 有可能有两个同名的不同变量吗?

Javascript 有可能有两个同名的不同变量吗?,javascript,jquery,variables,Javascript,Jquery,Variables,在jQuery中是否可能有两个同名的不同变量 我将jQuery链接到外部脚本。有可能有两个变量使用相同的字母名称吗 即: 包含在compressed.js中: var m = $$('li', s), 及 载于: 这两个变量是否相互影响?否,m和m2在javascript中被视为两个不同的变量 编辑: 根据您对原始问题的编辑,第一个问题m和第二个问题m肯定会相互影响。第二个变量定义是删除m的上一个值 具有相同范围和名称的两个变量总是会相互影响,或者,我想应该说:一个变量。好的,您的编辑会改变一

在jQuery中是否可能有两个同名的不同变量

我将jQuery链接到外部脚本。有可能有两个变量使用相同的字母名称吗

即:

包含在compressed.js中:

var m = $$('li', s),

载于:


这两个变量是否相互影响?

否,
m
m2
在javascript中被视为两个不同的变量

编辑:

根据您对原始问题的编辑,第一个问题
m
和第二个问题
m
肯定会相互影响。第二个变量定义是删除
m
的上一个值


具有相同范围和名称的两个变量总是会相互影响,或者,我想应该说:一个变量。

好的,您的编辑会改变一切。如果它们定义在同一范围内,则第二个将覆盖第一个。可能在代码中多次单独使用同一变量,但在不同的函数/作用域中使用。

如果两个变量在相同的作用域中具有相同的名称,它们将发生冲突,则后一个定义将覆盖前者

如果由于某种原因无法编辑变量名,可以将整个代码块包装在单独的匿名函数中:

$(function(){.....});

这将把两个变量放在不同的作用域中,只要你用
var
定义它们,它们就不会冲突。如果脚本的某些部分需要来自另一个脚本的变量,这可能会导致问题

简单的答案是否定的,它们不会相互影响

其工作原理是每个变量名称都是唯一的,因此
m
m1
m2
m3
m4
彼此之间没有任何影响:

但是,根据变量设置的值,您可以访问和更改数据,例如
m[2]
,原因是
m
是一个数组或某种类型的对象,您可以使用
[]
访问单个元素,这可能是您感到困惑的地方


变量如何相互影响的示例:

var a = 'hello';
var b = 'world';

alert(a); //hello
alert(b); //world

alert(a + b); //helloworld
alert(b + a); //worldhello

a = b;

alert(a); //world
alert(b); //world

b = 'hey';

alert(b); //hey
从上面的示例中可以看出,如果修改已设置变量的值,则会更改该值;如果尚未设置变量,则会更改其赋值

这是一个很好的技巧,您应该学习自调用匿名函数,我们使用这些函数的原因是创建一个框,将代码放入其中,这样它就不会影响框外的任何其他内容

范例

var hey = 'hey';

(function(){
    var hey = 'bye'; //This is only effective inside the {}

    alert(hey); //You get 'bye';

    //Access the global scope
    alert(window.hey); //You get 'hey';

    //modifiy the external scope
    window.hey = 'modified from within the function';

    //expose vars inside the function scope to the global window:
    window.exposed = hey; //Remember hey is internal and is set to 'bye';
})();

alert(exposed); //'bye';
alert(hey); //'modified from within the function'

希望您现在能理解多一点。

如果
m
的第二个实例具有相同的作用域,则它们将被覆盖。

您的代码令人困惑,您显示的变量具有不同的名称。@Dunhamzzz我复制错了。我的意思是问他们是否都是m。解决这个问题的最佳方法是什么,这是我的两个js文件www.etterengineering.com/compress.js,我看到的唯一解决方案是更改www.etterengineering.com/compress.js中的
m
变量。我看到的另一个有点复杂。。。
var hey = 'hey';

(function(){
    var hey = 'bye'; //This is only effective inside the {}

    alert(hey); //You get 'bye';

    //Access the global scope
    alert(window.hey); //You get 'hey';

    //modifiy the external scope
    window.hey = 'modified from within the function';

    //expose vars inside the function scope to the global window:
    window.exposed = hey; //Remember hey is internal and is set to 'bye';
})();

alert(exposed); //'bye';
alert(hey); //'modified from within the function'