Welcome to WebmasterWorld Guest from 35.173.57.202

Forum Moderators: open

Message Too Old, No Replies

dom variables

reusing variable names

     
7:45 pm on Sep 6, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


I have the following lines in a function:
//eg1 explicit creation of string object
var mystring = new String("doublespeak");
var myid = document.getElementById("results1");
var mypara = document.createElement("p");

var mystr = "The length of variable object myString: " + mystring + " is " + mystring.length + ".";
var mytextnode = document.createTextNode(mystr);

mypara.appendChild(mytextnode);
myid.appendChild(mypara);

//eg2 implicit creation of string object
var myotherstring = "doublethink";
mystr = "The length of variable object myOtherString: " + myotherstring + " is " + myotherstring.length + ".";

mypara = document.createElement("p");
mytextnode = document.createTextNode(mystr);

mypara.appendChild(mytextnode);
myid.appendChild(mypara);


Notice that I am reusing the variable names mystr, mypara and mytextnode in eg2 example. Are these variables still private since they were declared previously with the var keyword, and does it matter? And, is this the proper way to reuse the same variable name or should I use the var keyword on each variable? I mean, if I'm appending 50 paragraphs I'm sure I don't need to create 50 unique variable names.
9:17 pm on Sept 6, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15802
votes: 845


I've got a couple files totaling 150K that consist of nothing but variable declarations. (It's a weird page ;) Five-dimensional string arrays, that kind of thing.) Just checked, and I don't use the term "var" a single time. But maybe I've got a forgiving browser.

Making a variable global doesn't "lock in" its value, if that's what you mean. It can always get overwritten with anything new you feed it. Same goes for recycling variables within one function. If you've got a series of for/next loops, you probably call the outermost variable "i" or "count" or something like that every time. If you feel anxious you can always clear your variable ("" or 0, depending) before putting in a new value.
10:00 pm on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3146
votes: 3


The var keyword is used to declare the variable before it is first used. When used inside a function it will declare a 'local' variable (a property in the call object). If you don't use the var keyword then it will implicitly create a global variable (a property in the global object - the window object). IMO, you should still use the var keyword when declaring global variables when you are in the global scope (ie. outside of any function), although you don't need to in this instance.

When you later refer to the variable (previously declared with var), either using it in an expression or assigning another value to it then you don't use the var keyword, as it is already declared. So in your example, assuming all the code is in the same function then yes, they are all private to that function. You should use the var keyword everytime you declare a new variable and only when you declare a new variable.

var mystring = new String("doublespeak");


This does 2 things. First it declares the variable mystring AND then it initializes to a string object. And is the same as:

var mystring; 
mystring = new String("doublespeak");


if I'm appending 50 paragraphs I'm sure I don't need to create 50 unique variable names.


No, unless you needed to maintain a reference to each paragraph - but then you would probably use an array.
10:16 pm on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3146
votes: 3


An example...

var myString = 'Hello'; 
function helloWorld() {
var myString;
myString = 'World';
document.writeln(window.myString + ' ' + myString);
}
helloWorld(); // Outputs "Hello World"
10:23 pm on Sept 6, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


lucy24, thanks. I was just curious if I was changing the scope of the variable when reusing it without the var keyword. And, I was also wondering what best practice would be. I have many for and while loops and I have not used var either, just scared myself for a second thinking I was coding incorrectly.
10:38 pm on Sept 6, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


penders, excellent, I think I have it now. So is it acceptable to do:


while (conditionistrue){
var mypara = document.createElement("p");
var mytextnode = document.createTextNode("My content model.")
}


This would repeatedly initialize each variable, is this a problem? Or, should each var be declared before the while statement?
11:14 pm on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3146
votes: 3


I have many for and while loops and I have not used var either


Providing you have declared the variable using var earlier in the script, so that a local variable is declared rather than a global, then all good.

The for() loop is special in that it allows you to declare the variable using var inside the loop, in one tidy package...
for (var n=0; n<max; n++) {...}



while (conditionistrue) { 
var mypara = document.createElement("p");
var mytextnode = document.createTextNode("My content model.")
}


Yes, this is OK. It looks like the variables are repeatedly declared, however, I think the JavaScript interpreter must optimize this as the variables aren't redeclared.

However, it is good programming practise to always declare your variables at the start of your script - so you can easily see which variables are used in your script. (In some strongly typed languages you must declare all your variables at the start of your script.)


var mypara, mytextnode;
while (conditionistrue) {
mypara = document.createElement("p");
mytextnode = document.createTextNode("My content model.")
}
2:23 am on Sept 7, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15802
votes: 845


while (conditionistrue) {
var mypara = document.createElement("p");
var mytextnode = document.createTextNode("My content model.")
}

Do you actually need the vars here, or would it work equally well as just
document.createElement("p"); 
document.createTextNode("My content model.")

? That's assuming you don't actually use the very last paragraph and textnote for anything.

Don't know if this counts as good, bad or Personal Preference, but for me it works best to start out setting all variables as global. Except genuinely transitory ones, like counters in a loop. One less thing to think about at the beginning. Then once I see how all the functions interact I backtrack, follow each variable and figure out what needs to be passed to or returned from functions, and end up with next to no globals. The only things that stay global are the, er, named constants. (I assume there's a technical term which I don't know. Like, say, numberArtists = 8 but maybe next year it will be 9, so the code is written using numberArtists.)
4:24 am on Sept 7, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


I find the use of a js lint program useful in checking the correct use of var, and such things. I current use the one that comes as a notepad++ addon, it is convenient.
9:52 pm on Sept 7, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


Thanks all... I use to code COBOL and Mantis from the ancient world so I get it when you speak of strongly typed; loosely type js took some effort to comprehend (and as you may see, it's still making my head ache at times); and, I use notepad++ too so I'll be checking out this jslint thingy asap. Peace.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members