Jump to content

Global Variable Not Working


jimfog

Recommended Posts

I have a created a date object and assigned it to a global variable. It is in script, mainly written in jquery. All the code in such scripts(as you know) goes in to the document ready function. The problem is that this global variable(var d=new Date) the only way to be seen by the other functions of the script is ONLY if it is placed outside the document.ready function(which encloses everything else). Suppose i leave it there, is it going to have a negative impact regarding DOM loading? Many functions in my script use the date object.

Link to comment
Share on other sites

Yes, that is what the tutorials say,but in my case, it does not happen and i cannot understand why. As i said the only way to be seen as a global variable is if i take it outside the document.ready function.

Link to comment
Share on other sites

That's how global variables work. They need to be declared in the global space. (Ie, outside of any functions) If they are declared in any other namespace, they are not global.
unless it is defined without the var keyword. A variable declared anywhere without the var keyword defaults to the global space. Often cited by Crockford as arguably javascripts' worst part.
Link to comment
Share on other sites

unless it is defined without the var keyword. A variable declared anywhere without the var keyword defaults to the global space. Often cited by Crockford as arguably javascripts' worst part.
I suppose on a technicality basis that would be declaring a variable. In my mind, it is not a declaration unless it is accompanied by the var keyword. In such a case, it is just an assignment. The problem is JavaScript declares it for you in the global space if the variable doesn't already exist.
Link to comment
Share on other sites

if this function has a parameter called d, then that would be causing the issue, as it would assign the Date object to the local incoming variable, instead of a global one, but i doubt thats the case. - if so though, can be solved by justsomeguy's method. just to prove to you jimfrog that this works, i have been using this code over last 30 days:

$(document).ready(function() {	var mapCanvas = document.getElementById('map1')	if (!mapCanvas) return 	var penCanvas = document.getElementById('penCanvas')	if (!penCanvas) return 	map = new Map(mapCanvas, penCanvas, mapViewOnly)	map.setCanvasSize(576, 576)	...

and variable 'map' works perfectly as a global variable. personally i love how JS/Lua defaults to global variables without the 'var/local' word, and hate how PHP defaults to locals.

Link to comment
Share on other sites

If you want to declare a variable as global from inside a function, make it a property of the window object: window.d = new Date(). I advise against using Javascript's undeclared-variable-belongs-to-global feature, as it can be highly confusing to other developers reading your code, or even your own self in a few weeks looking back at the code.

Link to comment
Share on other sites

I have always developed from the perspective of trying to be as local/scoped as reasonably possible with my variables/functions/objects/etc unless (usually) properly and meaningfully namespaced.

Link to comment
Share on other sites

I managed to solve the issue after all in a way i did not expect and which certainly calls for someexplanation: I removed the var keyword and placed the variable above the functions that use it(in terms of order in the script).In the previous attempts i had removed the var keyword also but the variable was towards the end of the script. Now i just changed its order in the script. For example if functions 1,2,3 were in lines 10,20,30 respectively, i placed the variable in line 9. I did not know that the order/precedence play a role.

Link to comment
Share on other sites

if you declare a variable inside a function with the var keyword, then it will be scoped only within the confines of that functions scope, i.e. between the {}. Since that was what you did, the variable only existed in the function. A variable defined anywhere, i.e. x = 10, will become global no matter where it occurs in the code (since there is no var keyword before it). This also the same thing as defining it outside of any function scope with the var keyword. However, making use of lots of globals this is considered bad practice, and variables should be declared appriopriatly within the context of where they are going to be used. The trick is realizing what happens when you use the var keyword and in what context.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...