Closed Thread Icon

Topic awaiting preservation: variables and namespaces ? (Page 1 of 1) Pages that link to <a href="" title="Pages that link to Topic awaiting preservation: variables and namespaces ? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: variables and namespaces ? <span class="small">(Page 1 of 1)</span>\

Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-11-2001 16:24


I'm having bad times guessing problems like when using external script between my actual js code...example:

dx = 1234;
<script src='file1.js'></script>

now 'dx' will print other than 1234, why?! cause that 'file1.js' modified it...

most scripts we use use common variable namings like layername, layerwidth, i, x, y, dx, dy, etc etc....

can't I specify a name space when I include that external script and then specify another name space when using my own scripts? just to avoid such variables overwritings....

Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-11-2001 18:56

Not really
I haven't played to much with it but I usually consider all variables global.

You can define function specific variables by using the var statement with in the function. However any variables declared within the "global" space will be availible to all.

:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

Bipolar (III) Inmate

Insane since: Aug 2001

posted posted 10-11-2001 21:37

You can give your variables a sort of namespace like this. it is a bit long winded to set up the vars but you should be able to protect them from the external script.
Anyay, see what you think.

myVars = new Object();
//You always have to create variables with "myVars."
myVars.dx = 10;
myVars.dy = 10;
//create functions that don't conflict
myVars.hello = function(str)
alert("hello "+str);
<SCRIPT SRC="test.js"></SCRIPT>
<!--- suppose it contains the following code --->
dx = 1000;
dy = 1000;
function hello(str)
alert("Ola " + str);

//Use "with" as a shortcut to access your variables

dz = 10; // !!! Does not create !!!

dx += 5; //Does change the value of myVars.dx
alert(dx); //Output 15
//Now outside myVars namespace

alert(; //Output "undefined"
alert(dz); //Output 10 (not defined in myVars namespace)

I originally got this tip from somewhere in Doc Javascript

Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 10-11-2001 21:39

I'm not really sure because I don't use Namespaces in Javascript, however it looks like namespaces are supported in Javascript 2.0 from this link:

I hope that helps.

[This message has been edited by Bugimus (edited 10-11-2001).]

« BackwardsOnwards »

Show Forum Drop Down Menu