Oops, sorry for the weird formatting...
On Mon, Aug 3, 2009 at 6:38 PM, Ryan Gahl <[email protected]> wrote:
> Let me suggest the module pattern - which not inconsequently also enables
> true private fields and methods (just because it seems you're looking for
> alternative patterns). Also, since the module pattern allows for true
> private members, you can just create a helper function at the module level,
> thus avoiding placing the "f" function at the instance level. This assumes
> you probably don't want calling code to create and instance of Sub2 and then
> just randomly make calls to myInstance.f()...
>
> // (if you don't use a globally accessible namespace object to tack your
>> classes onto, then declare the class variable outside of the module function
>> so the reference is visible elsewhere, but I highly recommend just using a
>> namespace object to hold all your classes instead)
>
>
> var Sub2; //as in the note above, only required here if not using a
> namespace object
>
> (function() {
>
>
> //put the handler here, as a helper function, and no need for it to be
> public because you typically won't want outside code to do someInstance.f(),
> right? (no more of this "it has an underscore so it's private" stuff, people
> :)
>
>
> function f() {
>
> alert("Class:" + this.fu + this.baz);
>
> }
>
>
> Sub2 = Class.create(Super2, { // notice no "var" since in this
> example we declared outside the module so it's available outside the module
>
> baz: "bat",
>
> initialize: function () {
>
> document.observe("click", f.bind(this)); //just in-line the
> binding to make it cleaner, unless you need to re-user the bound function,
> then maybe cache it in a variable
>
> }
>
> });
>
> })();
>
>
> new Sub2();
>
>
>
>
>
>
> On Mon, Aug 3, 2009 at 12:05 PM, T.J. Crowder <[email protected]>
> wrote:
>
>>
>> Hi,
>>
>> > But it's really inelegant. How am I supposed to handle this?
>>
>> Pretty much like that, except I haven't reassigned the property like
>> that. It *should* be fine, you'll create an own property 'f' on the
>> instance (rather than an inherited one from the prototype) that's
>> bound. But it bothers me and I can't give you a good reason for
>> it. :-) I've done that sort of thing before (keep a bound copy around
>> on the instance, if I'm going to reuse it), I've just for some reason
>> always used a different prop name, I guess so I'm not in doubt whether
>> I've bound it. But doing it in the initializer like you have, there
>> wouldn't be any confusion...
>>
>> Also, the odds are pretty high you just want #bind, not
>> #bindAsEventListener.[1]
>>
>> [1]
>> http://proto-scripty.wikidot.com/prototype:tip-you-probably-don-t-need-bindaseventlistener
>>
>> HTH,
>> --
>> T.J. Crowder
>> tj / crowder software / com
>> Independent Software Engineer, consulting services available
>>
>>
>> On Aug 3, 2:39 pm, Cédric <[email protected]> wrote:
>> > Hello,
>> >
>> > I'm trying to use the Class methods to manage my object hierarchy on
>> > my current project, but I have some trouble with the "this" keyword in
>> > conjonction with Class.create.
>> >
>> > Here is a piece of plain-js code using prototypes to get inheritance:
>> >
>> > var Super1 = function () {
>> > this.fu = "bar";
>> >
>> > }
>> >
>> > var Sub1 = function () {
>> > this.baz = "bat";
>> > this.f = function () {
>> > alert("classic:"+this.fu+this.baz);
>> > }.bind(this);
>> > document.observe("click", this.f);};
>> >
>> > Sub1.prototype = new Super1();
>> > new Sub1();
>> >
>> > Here is my attempt at mimic this with Class.create:
>> >
>> > var Super2 = Class.create({
>> > fu: "bar"
>> >
>> > });
>> >
>> > var Sub2 = Class.create(Super2, {
>> > baz: "bat",
>> > f: function () {
>> > alert("Class:"+this.fu+this.baz);
>> > }.bindAsEventListener(this),
>> > initialize: function () {
>> > document.observe("click", this.f);
>> > }});
>> >
>> > new Sub2();
>> >
>> > But of course it doesn't work, f in bound to window, not the object
>> > create by new. The only way I found is:
>> >
>> > var Super2 = Class.create({
>> > fu: "bar"
>> >
>> > });
>> >
>> > var Sub2 = Class.create(Super2, {
>> > baz: "bat",
>> > f: function () {
>> > alert("Class:"+this.fu+this.baz);
>> > },
>> > initialize: function () {
>> > this.f = this.f.bindAsEventListener(this);
>> > document.observe("click", this.f);
>> > }});
>> >
>> > new Sub2();
>> >
>> > But it's really inelegant. How am I supposed to handle this?
>> >>
>>
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---