An undeclared variable is any that is first defined within any given scope without the use (directly or indirectly) of the var keyword. So, in your blend function, we have at least these:
Code:
fullpath = 'images/full/';
orgpath = 'images/original/';
loadingid = 'loading';
loadingpic = '..images/loading.gif';
to use the var keyword directly in declaring those, would be:
Code:
var fullpath = 'images/full/';
var orgpath = 'images/original/';
var loadingid = 'loading';
var loadingpic = '..images/loading.gif';
to use it indirectly, it has to be use directly on at least one previous variable in a comma separated chain of variable declarations:
Code:
var fullpath = 'images/full/',
orgpath = 'images/original/',
loadingid = 'loading',
loadingpic = '..images/loading.gif';
or even:
Code:
var fullpath = 'images/full/', orgpath = 'images/original/', loadingid = 'loading', loadingpic = '..images/loading.gif';
When one doesn't do this one way or another, the variable is defined in the global scope, and may conflict with other items there, including other variables and elements with id's that match the variable name.
Even if the scope is intended to be global, it should be formally declared with var in the global scope, so as not to conflict with an element. Once declared globally, it can be defined later inside a function without the var keyword and will maintain its new value globally unless later defined again.
The scope of a variable is the level of recognition within a given function or set of nested functions that you need it to have.
Code:
var blah='Hi';
function doit(){
alert(blah)
}
doit();
will alert blah from the global scope, or Hi.
Code:
var blah='Hi';
function doit(){
var blah='Bye';
alert(blah)
}
doit();
alert(blah);
Now we get Bye as defined in the local scope of the doit() function, followed by a second alert of Hi, from the unaffected global scope.
Code:
var blah='Hi';
function doit(){
blah='Bye';
alert(blah)
}
doit();
alert(blah);
This time we get two Bye alerts, because doit()'s blah used the global scope.
Bookmarks