From: Stevo on
Scott Sauyet wrote:
> On Feb 12, 2:53 pm, vunet <vunet...(a)gmail.com> wrote:
>> I found the library with this constructor below. I would like to
>> understand why is it used like this: within (function(){})();
>> constructor and no var declaration for SomeGlobalVar variable. What
>> are the advantages and what does it mean?
>
> The basic idea is that global variables are evil. This technique
> moves what would otherwise be global variables into a closure [1],

> As to how closures manage to keep the global namespace clean, you will
> need to read up on closures. [1]
> Cheers,
> -- Scott

You might want to read up on closures too. Using that syntax discussed
doesn't automatically create a closure. A closure is only created if it
returns a reference to a variable or function. A closure is where a
reference is kept to something that might otherwise have been destroyed
or garbage collected but is kept alive due to the external reference to
it. Well, that's how I understand them anyway.
From: Gregor Kofler on
vunet meinte:
> Thank you very much. Would you mind me asking for some brief
> clarifications?
>
>> All functions and variables declared inside this block will remain
>> "private", i.e. not visible from the outside.
>
> 1. But can we call SomeGlobalVar.doIt() from outside if it is set
> like:
>
> SomeGlobalVar = (function () {
> doIt : function(){}
> })();

No.
But you can return an object with a callable property doIt():

SomeGlobalVar = (function () {
return { doIt : function(){} };
})();

> 2. and why keyword "var" is omitted? Is it needed at all?

Not necessarily. without "var" SomeGlobalVar will end up - as the name
suggests - in the global scope.

Gregor


--
http://www.gregorkofler.com
From: Gregor Kofler on
vunet meinte:
> Thank you very much. Would you mind me asking for some brief
> clarifications?
>
>> All functions and variables declared inside this block will remain
>> "private", i.e. not visible from the outside.
>
> 1. But can we call SomeGlobalVar.doIt() from outside if it is set
> like:
>
> SomeGlobalVar = (function () {
> doIt : function(){}

You meant

var doIt = function() { ... };

- right?



--
http://www.gregorkofler.com
From: vunet on
On Feb 12, 4:36 pm, Gregor Kofler <use...(a)gregorkofler.com> wrote:
> vunet meinte:
>
> > Thank you very much. Would you mind me asking for some brief
> > clarifications?
>
> >> All functions and variables declared inside this block will remain
> >> "private", i.e. not visible from the outside.
>
> > 1. But can we call SomeGlobalVar.doIt() from outside if it is set
> > like:
>
> > SomeGlobalVar = (function () {
> >      doIt : function(){}
>
> You meant
>
> var doIt = function() { ... };
>
> - right?
>
> --http://www.gregorkofler.com

No I meant
var globalObj = (function() {
doIt : function(){
alert("doIt executed");
}
})();
From: vunet on
On Feb 12, 5:05 pm, vunet <vunet...(a)gmail.com> wrote:
> On Feb 12, 4:36 pm, Gregor Kofler <use...(a)gregorkofler.com> wrote:
>
>
>
> > vunet meinte:
>
> > > Thank you very much. Would you mind me asking for some brief
> > > clarifications?
>
> > >> All functions and variables declared inside this block will remain
> > >> "private", i.e. not visible from the outside.
>
> > > 1. But can we call SomeGlobalVar.doIt() from outside if it is set
> > > like:
>
> > > SomeGlobalVar = (function () {
> > >      doIt : function(){}
>
> > You meant
>
> > var doIt = function() { ... };
>
> > - right?
>
> > --http://www.gregorkofler.com
>
> No I meant
> var globalObj = (function() {
>         doIt : function(){
>                 alert("doIt executed");
>         }
>
> })();
>
>

Yes, sorry you are right. My syntax is incorrect.