views:

228

answers:

5

Hi All,

Many thanks in advance. I'm working out of a schoolbook and they're using one function to call another which opens a window:

function rtest(){
   content='dans window';
   oneWindow=open("","Window 1","width=450,height=290");
   newWindow(oneWindow);
}
function newWindow(x){
   x.document.close();
   x.document.open();
   x.document.write(content);
   x.document.close();
   x.moveTo(20,20);
   x.focus();
}

So everything works fine, but my question is this: how is the newWindow() function able to access the contents of the "contents" variable in the rtest() function? And why, if I preface the "content" variable with "var", like this:

function rtest(){
  **var content='dans window';**
  oneWindow=open("","OneWindow","width=450,height=290");
  newWindow(oneWindow);
}

...does an error get thrown (and the contents of the new window left blank)?

Can anybody explain to me what the difference between using var and not using var is?

Thank you!

Daniel

+5  A: 

if you dont use var inside the rtest, it is automatically a global variable. which is why it is accessible from other javascript codes including newWindow. now, when you use var, it automatically a variable inside the rtest scope, so the ones that can use it now are those inside the same scope.

Eugene Ramirez
Thanks, Eugene -- I suspected as much. I'm working with the CIW JavaScript Fundamentals student guide from the actual CIW curriculum, and <i>nowhere</i> in this chapter does it say <i>anything</i> about the variable being global through the omission of "var".Thank you, sir!
ajax81
A pedant notes: an undeclared variable in a function doesn't exactly become a global variable: instead, it becomes a property of the global object. For most situations this is the same thing, however.
Tim Down
+4  A: 

If you declare the variable using var inside the original function, it will become a local variable and will not be visible outside the function.

If you don't declare the variable at all, it will be global. However, best practice is to declare global variables. If your textbook doesn't do this, consider replacing it. If your professor doesn't do this, consider replacing (or reforming) him. :-) If you have trouble convincing him, you can (but not necessarily should) mention that I'm one of the top 200 users here.

For example:

var content;

function rtest(){
    content='dans window';
    oneWindow=open("","Window 1","width=450,height=290");
    newWindow(oneWindow);
}

Also, the best way to open a blank window is to call open("about:blank", ...), not open("", ...).

SLaks
I would consider replacing professor on the fact that he's using global variables, no matter does he declare them or not.
vava
In an introductory course, it's not so bad.
SLaks
However, he should discuss namespaces and closures later on.
SLaks
Shouldnt the variable be available in the context of newWindow because it is passed to that function explicitly in the newWindow(oneWindow) call, meaning that whether declared locally or globally it should be available as x in "newWindow"
GrayWizardx
"If you have trouble convincing him, you can mention that I'm one of the top 200 users here." I can just imagine that conversation !
Pool
Read more carefully. There are two different variables here.
SLaks
oops my bad, I did not notice that it was "content" that was being declared as local, not oneWindow. Disregard.
GrayWizardx
+1  A: 

It's about the function-scope, if you declare your variable with var, it will be available only in the scope of the function where you did it.

If you don't use the var statement, and you make an assignment to an undeclared identifier (an undeclared assignment), the variable will be added as a property of the Global object.

CMS
A: 

With var you declare a local variable in the function which is thus not visible outside this function. Without var you are actually working on the window object and set or overwrite a field of it. Your global scope in client side Javascript is always the window object. So you could also have written window.content='dans window'; to make clearer what you are actually doing there, but otherwise it would be identical. By the way: the window variable is itself just a field of the window object that refers recursively back to the window.

x4u
+1  A: 

If you don't use var, then you are creating a global variable; that is, a variable that is accessible from any code anywhere in your program. If you use var, you are creating a local variable, which is a variable that is only accessible from within the scope in which it is defined (generally, the function it is defined in).

While global variables can be convenient at first, it's generally a bad idea to use them. The problem is that all of your code will share that one global variable; in the future, if you need to have two or more different versions of that variable for whatever reason, you won't be able to separate the two uses. Global variables can also be accessed or changed from anywhere within your program, so it can be hard to figure out what might be modifying or depending on one, while local variables can only be accessed within a limited, well defined section in code, which can easily be inspected.

Brian Campbell
An undeclared variable in a function doesn't exactly become a global variable. See my comment on Eugene Ramirez's answer. Also, a variable declared using `var` in the global scope is a global variable, so it is the scope rather than the use of `var` that determines whether a variable is global or not.
Tim Down