Global Namespace in JavaScript vs Java -


i came across line 'in javascript, code shares single global namespace'. want confirm if understanding correct.

my understanding:

in java:

application 1 -> (has variable called) outputtext = "morning"

application 2 -> (has variable called) outputtext = "evening"

since 2 projects have different memory allocations, have different namespace containers, , there no danger of variables getting mixed up. printing application1.outputtext gives "morning", , application2.outputtext gives "evening".

in javascript:

application 1 -> (has variable called) outputtext = "morning"

application 2 -> (has variable called) outputtext = "evening"

since share same namespace containers, application1.outputtext may give "evening".

is understanding correct?

thanks

it depends on mean "application", short answer yes, correct.

the longer answer normal javascript "best practice" scope variables. global namespacing applies variables/functions aren't declared within function (or self evoking function) or object property.

so this:

var outputtext = "morning"; 

then in file somewhere...possibly in poorly written api:

var outputtext = "evening";  

will contain value of whichever of encountered last when executing.

what in javascript things like:

var application1; application1.outputtext = "morning"; 

and somewhere else:

var application2; application2.outputtext = "evening"; 

or, similarly, since in javascript object, including functions:

function application1(){     var outputtext = "morning"; } function application2(){     var outputtext = "evening"; } 

with latter two, have separated variables , different results.


Comments