This is the biggest change to get used to with Javascript if you came from a
"typical" OO environment. I'm going to emphasize it again for other people
reading this thread:

in javascript, HOW a function is called is more important than WHERE it gets
called or WHAT is calling it.

For further reference, check out Mozilla's docs on function.apply [1]. It'll
give you insight into what .bind() is really doing behind the scenes, and
why that context parameter is important.

-Jerod

[1]
http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply

On Fri, Oct 17, 2008 at 3:42 PM, T.J. Crowder <[EMAIL PROTECTED]>wrote:

>
> > It's kind of understandable why context would
> > behave the way scoping (or rather identifier resolution) works.
>
> Oh, completely.  It's one of the big, big adjustments people like me
> have to make coming to JavaScript from C++, Java, C#, etc.  All I
> meant was, having made that adjustment (er, mostly) and with my
> JavaScript hat on, I wasn't surprised to have the context of each()'s
> iterators default to the global object, because that's what I'm used
> to *in JavaScript*.  If I were coding in Java, it would be very
> surprising indeed.
>
> No, the whole question of whether the behavior of JavaScript's 'this'
> "pseudo-parameter" (as Crockford calls it) is surprising is a
> different thing entirely.  *I* was certainly surprised by it at first,
> I don't mind saying. ;-)
>
> -- T.J. :-)
>
> On Oct 17, 8:33 pm, kangax <[EMAIL PROTECTED]> wrote:
> > On Oct 17, 12:08 pm, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > > It's intuitive because it's like closures work.
> >
> > > Not really, or at least, not if I understand what you mean.  This
> > > code, for instance:
> >
> > > var Thingy = Class.create({
> >
> > >     initialize: function(name)
> > >     {
> > >         this.name = name;
> > >     },
> >
> > >     showName: function()
> > >     {
> > >         alert("this.name = " + this.name);
> > >     },
> >
> > >     doSomething: function()
> > >     {
> > >         (function(){
> > >             alert("this.name = " + this.name);
> > >         })();
> > >     }
> >
> > > });
> >
> > > If I call it like this:
> >
> > >     var t;
> > >     t = new Thingy('Fred');
> > >     t.showName();
> > >     t.doSomething();
> >
> > > ...first alerts "this.name = Fred" (from showName) and then "this.name
> > > = " (from doSomething).  The closure inside doSomething() does not
> > > inherit "this" from the scope in which it's called, so "this" is
> > > "window" within it.  Like all other functions, "this" is determined by
> > > *how* it's called, not where.
> >
> > > Granted closures do inherit other aspects of context (in-scope vars
> > > and such), so if that's what you mean, I take your point.  But I think
> > > that's a bit apples and oranges.
> >
> > Well, some people do indeed expect *context* to behave as *scope*.
> > Some people also confuse these two or believe that one depends on
> > another in some way : / It's kind of understandable why context would
> > behave the way scoping (or rather identifier resolution) works. Maybe,
> > if context was set to the caller's one, we wouldn't have all this
> > binding mess we have now.
> >
> > IIRC, Crockford was one of the first people to "complain" about
> > context semantics (proposing something along the lines of what Tomasz
> > expected)
> >
> >
> >
> > > > I think you should use simple example in like mine in API Docs to
> > > > illustrate it's not intuitive.
> >
> > > Just my two pennies worth, but it worked the way *I* expected it to.
> > > I haven't been using Prototype that long and I remember not being
> > > surprised by this, because it worked the way functions in general
> > > work:  I didn't explicitly supply a context, and it got the global
> > > object.  So I found it "intuitive" and would be surprised if it were
> > > something else without me telling it to be.  Just FWIW.
> > > --
> > > T.J. Crowder
> > > tj / crowder software / com
> >
> > [snip]
> >
> > --
> > kangax
> >
>


-- 
Jerod Venema

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to