"withn the flow of text"? Still not getting the usage, maybe you can
point me to a specific reference of "inline" as common usage in
JavaScript. Inline in common JavaScript usage (since it is 99.9% of
the time DOM related) usually refers to javascript (usually event
related) within markup, such as an "onclick=function(){}" in an anchor
tag.

If in your example bar() is "inline" to foo(), how does that make an
anonymous, self-invoking function "inline"
<script>
(function(){alert('hello');})()
</script>
i.e, what flow is it "inline" to, other than the script tag containing
it, or the window (in which case everything is inline, and the term
would lose all meaning)?

Saying "That line defines a function inline: " in reference to the
OP's example (an anonymous, self-invoking function) when it was given
outside the context of any other text implies that type of
construction is somehow 'inline'. At the very least it begs the
question of how you'd write the OP's example not to be inline?

I also still don't understand your use of vars (yes, you did say
'vars' and not 'properties') so the question stands, what does this
mean?:

"That sort of thing is frequently used to scope the vars inside rather
than having them populate the containing scope."

A self-invoking, anonymous function has nothing to do with the scope
of the vars inside it; their scope is defined by how you declare them.
Liking implicit globals or not (most people including myself think
they're a bad idea), is beside the point;  someone new to JavaScript
reading the above sentence would, I think, be mislead as to how scope
is determined. It doesn't matter how deep you nest them, variable
scope is lexical to a function only if you declare it with var:

(function(){ //foo2 & test2 are global
    var  foo1 = "bar";
    foo2 = "boo";
     (function(){var test="one";test2="two";})()
})();

For the OP's edification, the jQuery library uses the anonymous, self-
invoking function itself, and takes advantage of the fact that a
variable declared within it without the var declaration is global, in
order to make the jQuery object globally available. Also, as
JavaScript is a truly functional language, and although it got some
things wrong (like global variables), it got some things REALLY right.
One of those things is lambda; it's the first lambda language to get
mainstream adoption. In JavaScript functions are objects, and can be
passed to other functions as arguments; the example I gave in the
previous post of passing jQuery as an argument to the anonymous
function is an example of this, and it's really powerful.

On Mar 15, 10:40 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> > Not sure what you mean "inline" or by "scope the vars inside";
> > variables declared inside the function are scoped "inside"
>
> Yes.  Isn't that what I said?
>
> Re inline:  I meant inline as in...well...inline. ;-)  I'm not sure
> how else to say it; within the flow of the text rather than outside
> it.  E.g., here's a function that's inline within another function:
>
> function foo() {
>
>     /* ...do some stuff in foo... */
>
>     function bar() {
>     }
>
>     /* ...do some more stuff in foo... */
>
> }
>
> bar() is defined inline, within foo().  (In the above case, just
> defined rather than defined and called.)  I mean, technically, *all*
> JavaScript functions are inline in some sense. :-)  But common usage
> as I've seen it refers to inline functions when they're within the
> flow of something.  (As opposed to the common usage in compiled
> languages, referring to a compiler optimization and [sometimes] a
> language feature allowing you to try to direct the compiler.)
>
> > ...as long as they are preceded with the
> > var declaration (if not, they are global, even with this format).
>
> Yes, I said "vars" not "properties".  Don't get me started on the
> horror of implicit globals.[1] ;-)
>
> [1]http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html
>
> -- T.J.
>
> On Mar 15, 5: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).
>
> > The closing/end parens creates a self-invoking, anonymous function.
> > The function is called immediately and returns whatever is inside;
> > sometimes you'll see jQuery called this way:
>
> > (function($){
>
> >   /* this variable is only available within the function, unless you
> > create a closure */
> >   var foo1 = 'bar';
>
> >  /* this variable has global scope and can be accessed later */
> >   foo2 = 'boo';
>
> > })(jQuery);
>
> > In this case, it's anonymous (the function has no name and isn't
> > assigned to a var), and self-invoking (the parens at the end invoke
> > the function as in T.J. Crowder's f() example), and it additionally is
> > passing an argument to the function (the jQuery object--which is
> > global, which is then assigned to the $ inside the function). The foo#
> > variables inside have different scope as indicated. A variable
> > declared inside a function with var can still be accessed outside the
> > function by means of a closure.
>
> > On Mar 15, 6:35 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
>
> > > Hi,
>
> > > On Mar 15, 12:55 pm, lovespring <iamd...@gmail.com> wrote:
>
> > > > (function(){})();
> > > > does it conform to the grammar?
>
> > > Yes, it does.  That line defines a function inline:
>
> > >     (function(){})
>
> > > ...and then calls it immediately using () like any other function:
>
> > >     (function(){})();
>
> > > (You need the parens around the definition so the () gets applied to
> > > the right expression.)
>
> > > Other than the obvious fact I'm using a symbol 'f' here, it's
> > > identical to:
>
> > >     var f = function(){};
> > >     f();
>
> > > That sort of thing is frequently used to scope the vars inside rather
> > > than having them populate the containing scope.
>
> > > HTH,
> > > --
> > > T.J. Crowder
> > > tj / crowder software / com
> > > Independent Software Engineer, consulting services available

Reply via email to