Is it possible to have two different variables with the same name?
Is it possible to have two different variables in with the same name in jQuery?
I have my jQuery linking to outside scripts. Is it possible to have two variables using the same letter name?
ie:
Contained in compressed.js:
var m = $$('li', s),
and
Contain开发者_运维百科ed in http://www.google.com/jsapi:
var m = "push",
Are these two affecting each other?
If 2 variables have the same name in the same scope they will clash, the latter definition will overwrite the former.
If for some reason you cannot edit the variable names, you could wrap the entire blocks of code in separate, anonymous functions:
$(function(){.....});
This will put the 2 vars in separate scopes as long as you define them with var
, so they will not clash. This may cause issues if parts of your scripts need the vars from the other.
The simple answer is no, they will not effect each other.
how it works is that each variable name is unique, so m
,m1
,m2
,m3
,m4
does not have any affect to each other what so ever:
However depending on the value set to the variable you may access and change data like so m[2]
the reason for this is that m
is an array or an object of some kind, and you can access the individual elements by using the []
which is probably where your getting confused.
Example of how variables effect eachother:
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
as you can see from the examples above if you modify the value of a variable that has already been set it changes the value, if the variable has not already been set its assigned the value;
a nice trick that you should learn self calling anonymous functions, the reason we use these is to create a box to put our code inside so it does not effect anything else outside the box.
Example
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'
hope you understand a little more now
No, m
and m2
are counted as two different variables in javascript.
EDIT:
Based on your edit of you orginial question m
and the 2nd m
are DEFINITLY affecting each other. The second variable definition is erasing the previous value of m
.
Two variables with the same scope and name will always affect each other - or, I suppose should be said: the one variable.
The second instance of m
will be overwritten if they both have the same scope.
Ok your edit changes things. If they are defined in the same scope than the second will overwrite the first. It is possibly to use the same variable independently multiple times in your code, but in separate functions/scopes.
精彩评论