[Proto-Scripty] Re: Enumerable context - bug?

2008-10-18 Thread Tomasz Kalkosiński

On 17 Paź, 22:07, Jerod Venema [EMAIL PROTECTED] wrote:
 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:

Exactly. But I get your point that's JavaScript world and things go
slightly different here (more than slightly actually :D).

For OO newcomers you should add example This will NOT work below
each() in API Docs and explain why. These counter-examples helps a lot
(especially Event.stopObserving was very helpful for me - for sure it
saved a day or two).

Thank you for these detailed explanations and keep up the good work :)

Greetings,
Tomasz Kalkosiński
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Enumerable context - bug?

2008-10-17 Thread kangax

On Oct 17, 7:35 am, Tomasz Kalkosiński [EMAIL PROTECTED]
wrote:
 Hey guys.

 For Enumerable, according to API Docs I've found: If there is no
 context argument, the iterator function will preserve the scope it
 would have anyway.

Sorry, that's just a poor wording on our side : /
By preserve scope it would have anyway it was probably meant that a
function is not explicitly bound to any object.
What actually happens is simple. The function (e.g. `Enumerable.each`)
always invokes its iterator via `apply` (passing it an explicit
`context` argument). When that `context` argument is `undefined`,
function is called within a global context (see 15.3.4.3 in ecma-262
specs).

The way you understood binding in this case is actually pretty
intuitive to many people. The problem is that callee has nothing to do
with caller's context (at the moment when it's being called). Caller's
context is never preserved when callee enters its own execution
context. There's not much we can do to make it work the way you
describe : )

[snip]


 Greetings,
 Tomasz Kalkosiński

--
kangax
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Enumerable context - bug?

2008-10-17 Thread Tomasz Kalkosiński

On Oct 17, 3:44 pm, kangax [EMAIL PROTECTED] wrote:
 Sorry, that's just a poor wording on our side : /
 By preserve scope it would have anyway it was probably meant that a
 function is not explicitly bound to any object.

 The way you understood binding in this case is actually pretty
 intuitive to many people.

It's intuitive because it's like closures work. Now I have doubts if
my other Enumerable methods behave as I intended them to. What do
others think?

I think you should use simple example in like mine in API Docs to
illustrate it's not intuitive.

I'm sad :(
Tomasz Kalkosiński


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Enumerable context - bug?

2008-10-17 Thread T.J. Crowder

 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
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---