ryan.od Posted January 20, 2007 Share Posted January 20, 2007 I am new to JS and am reading a book that says the following:[code]var iBaseNum = 10;function addNumbers(iNum1, iNum2) { function doAddition() { return iNum1 + iNum2 + iBaseNum; } return doAddition();}[/code]The book then says the variable iBaseNum is a global variable. My understanding is that to declare a global variable in ECMAScript, you leave off the 'var'. Can anyone explain this to me? Thank you. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted January 21, 2007 Share Posted January 21, 2007 if you use the 'var' outside of any function, then the variable is global and it's current value can be accessed inside/outside any function. If you use the 'var' inside of a function, then the variable is local to that function.[code]var test = 10;document.write test(); //prints 5 not 10!document.write test2(); //prints 10function test() { var test = 5; //local variable, overrides gobal variable for this function only! return test; //returns local variable value}function test2() { return test; //returns global variable value}[/code] Quote Link to comment Share on other sites More sharing options...
bibby Posted January 22, 2007 Share Posted January 22, 2007 But [b]NOT[/b] using var , even from within a function, puts it in the global scope. Quote Link to comment Share on other sites More sharing options...
ryan.od Posted January 22, 2007 Author Share Posted January 22, 2007 Ok, so is there any reason to pass variables declared outside of a function into that function?? It seems to me that unless I am passing local variables from one function to another, I have no need to pass variables declared outside of functions since they are all global.Moreover, everyone always screams that global variables are bad, bad, bad. How come every variable in JS declared outside of a function is global? Does the same hold true in Java, C++, Ruby, etc.?Ryan Quote Link to comment Share on other sites More sharing options...
mainewoods Posted January 22, 2007 Share Posted January 22, 2007 you have a need to pass variables declared globally in the parameters if you use the function with differents vars:[code]function cleancode($html) { //a function to remove extraneous spaces, tabs, line feeds ect. from html code // I would call it with different strings // fill in actual code return $cleanedcode;}[/code]who says you shouldn't use global vars? Professors in invory towers? Quote Link to comment Share on other sites More sharing options...
fenway Posted January 22, 2007 Share Posted January 22, 2007 Javascript has the worst scoping I have ever seen, with very strange quirks as a result -- it's just good programming style to pretend that it should work the way it's supposed to. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted January 23, 2007 Share Posted January 23, 2007 Are you saying that you shouldn't use global variables at all fenway because I've never heard that preached that much in articles I've read. You could of course go overboard in usage of them but are you saying - no usage. On my pages I use a few global vars that maintain the current state of the page and might be accessed in multiple functions in the page. Those functions have parameters passed to them as well as accessing some global vars. I could have passed the global vars as function parameters but using them globally just saves coding. If global vars are bad them what is the suggested alternative? Quote Link to comment Share on other sites More sharing options...
fenway Posted January 23, 2007 Share Posted January 23, 2007 Just be consistent... if it's global, don't pass it. Just realize that if you ever want to use the function somewhere else, you'll need to introduce another global... why bother? It makes much more sense not to use them. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.