On 3 août, 22:38, Matt Foster <mattfoste...@gmail.com> wrote:
> Regardless of the JS framework.  A closure is necessary for attaching
> class methods to a particular instance and preserve the instance
> reference via the "this" keyword.
>

This is automatic with the regular "Constructor" syntax. Defining a
function as an object method inside a constructor creates a closure,
so we can use #bind at the same time as the definition.

The point is, Class.create acts like a prototype more than as a
constructor. I never really thought of that. So I can't bind to my
object instance inside Class.create. And I guess that what acts as a
constructor in Class is the initialize method... So, maybe, I should
define f inside initialize, something like:

var Sub4 = Class.create(Super4, {
        baz: "bat",
        initialize: function () {
                this.f = function () {
                    alert("Class:"+this.fu+this.baz);
                }.bind(this);
                document.observe("click", this.f);
        }
});

Is there a cost to operating that way? I guess I won't be able to use
Class.addMethods...

> I thought Function.bind was pretty clean myself, but I guess you've
> got the sweet tooth for the syntax.

I think #bind is awesome :) It's the "bind the function in the
initializer" that bothers me, when I compare it to the standard syntax
"bind the function at its definition".

--~--~---------~--~----~------------~-------~--~----~
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 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to