I think this post has gone way off course, and yet there really isn't
any disagreement among us; it's mostly imprecision in language giving
rise to ambiguity :P

My apologies to lovespring for making a simple question require so
much scolling :)

So to try and bring this back to the OP:

1.does it conform to the grammar? Yes. It's an anonymous, self-
invoking function (look no further than the jQuery library if you want
an example).
2. why coding like this? Any variables declared inside this function
(or other contained functions) with the var keyword will be scoped to
those functions according to rules of the scope chain. This allows you
to keep your code encapsulated in a way that doesn't pollute the
global namespace.

Other methods of creating functions outside of the anonymous, self-
invoking function

var foo = function(){...}
or
function foo(){...}

and called by foo();

create a global variable, foo in this example, which can be
overwritten by some other code that happens to decalre a global var
'foo'.

I suggest for further edification you look at the jQuery library code,
look at jQuery.noConflict() as an example of having to deal with
conflicting global variable names (the beloved $), and Google scope,
closure, and functional javascript in general.

On Mar 15, 3:31 pm, mkmanning <michaell...@gmail.com> wrote:
> " x=3; // This does _not_ change the global x!"
>
> No, it doesn't change the global, because it's declared with var in
> the function first, so x in the function isn't global, it's lexically
> scoped to the function; x in the inner function is lexically scoped to
> the containing function (x within the outer function will be 3 instead
> of 2). Here's the really important thing: x is no longer 1 in the
> outer function because of the variable declaration:
>
> var x = 1;
> (function() {
>     console.log(x);
>    var x = 2;
>     console.log('outer function '+x);
>         (function() {
>         x=3; // This does _not_ change the global x!
>                 console.log('inner function '+x);
>     })();
>         console.log('outer function changed to '+x);})();
>
> console.log('outside the functions still '+x);
>
> This will give you:
> undefined //<-- note this!
> outer function 2
> inner function 3
> outer function change to 3
> outside the functions still 1
>
> Most inexperienced programmers would expect x to be 1 in the first
> console.log(), but it's not; its scope is no longer global because of
> the var declaration that follows it.
>
> Sorry if my wording was confusing or overly simplifed; understanding
> scope, closures, etc. takes more than a couple of sentences in
> general. But what I said is in effect true:
>
> "variables declared inside the function are scoped 'inside' (they have
> lexical scope to the function), as long as they are preceded with the
> var declaration". Add to this 'at least once.', although I'm not sure
> even then it describes the picture accurately enough for a newcomer to
> JavaScript who doesn't understand the scope chain.
>
> This still digresses from my original point: mostly I was responding
> to the statement in T.J. Crowder's post that implied that the
> anonymous, self-invoking function defined the scope of a variable. It
> doesn't.
>
> On Mar 15, 1:19 pm, Matt Kruse <m...@thekrusefamily.com> wrote:
>
> > On Mar 15, 12:00 pm, mkmanning <michaell...@gmail.com> wrote:
>
> > > Not sure what you mean "inline" or by "scope the vars inside";
> > > variables declared inside the function are scoped "inside" (they have
> > > lexical scope to the function), as long as they are preceded with the
> > > var declaration (if not, they are global, even with this format).
>
> > Be careful... this is not true.
>
> > Variables are always resolved up the scope chain. By prefixing a
> > variable with 'var' it just creates a property in the current scope by
> > that name, which will be resolved first. Variables not prefixed by
> > 'var' do _not_ automatically refer to 'global' variables, they are
> > just not defined in the local scope and therefore need to be resolved
> > by looking up the scope chain.
>
> > Example:
>
> > var x = 1;
> > (function() {
> >     var x = 2;
> >     (function() {
> >         x=3; // This does _not_ change the global x!
> >     })();})();
>
> > alert(x);
>
> > Some may consider this to be semantics, but it's important. :)
>
> > Matt Kruse

Reply via email to