Closed Thread Icon

Topic awaiting preservation: variables and namespaces ? (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=7876" 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>\

 
lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-11-2001 16:24

Hello,


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

<script>
dx = 1234;
</script>
<script src='file1.js'></script>
<script>
alert(dx);
</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....



bitdamaged
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. ]:

RoyW
Bipolar (III) Inmate

From:
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.

code:
<SCRIPT>
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>
<SCRIPT SRC="test.js"></SCRIPT>
<!--- suppose it contains the following code --->
<SCRIPT>
dx = 1000;
dy = 1000;
function hello(str)
{
alert("Ola " + str);
}
</SCRIPT>

<SCRIPT>
//Use "with" as a shortcut to access your variables
with(myVars)
{
alert(dx);
alert(dy);
hello("world");

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

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

alert(myVars.dz); //Output "undefined"
alert(dz); //Output 10 (not defined in myVars namespace)
hello("world");
</SCRIPT>


I originally got this tip from somewhere in Doc Javascript http://www.webreference.com/js/

Bugimus
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:
http://www.mozilla.org/js/language/js20/core/namespaces.html

I hope that helps.



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

« BackwardsOnwards »

Show Forum Drop Down Menu