> I don't see anywhere that a's `to_execute` property is set,

ah yeah, that was a typo..  That should have been

initialize: function() {
        new b(this.future_func)
},

Thanks for the links..  doing new b(this.future_func.bind(this))
worked perfect!

-patrick



On Nov 14, 6:56 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> There are a couple of problems there, not least that I don't see
> anywhere that a's `to_execute` property is set, so as far as I can see
> b would get an undefined value. Did you mean `do_something`?
>
> But I suspect that's a copy-and-paste error. The basic problem is that
> you're passing a function without any context. Functions are just
> functions, the `this` value does not follow them around as it (seems
> to) in some other languages. In JavaScript, `this` is set purely by
> how you *call* a function. There are several ways to deal with that,
> including Prototype's Function#bind[1].
>
> I wrote up a full description of this issue[2] in my blog a while
> back.
>
> [1]http://api.prototypejs.org/language/function.html#bind-instance_method
> [2]http://blog.niftysnippets.org/2008/03/mythical-methods.html
>
> HTH,
> --
> T.J. Crowder
> Independent Software Consultant
> tj / crowder software / comwww.crowdersoftware.com
>
> On Nov 14, 8:06 am, patrick <patrick99...@gmail.com> wrote:
>
> > Can anyone explain this to me?   It appears to be a scope issue where
> > my_a no longer is set to the instantiated class 'a' once it gets
> > executed in class 'b's initialize method...  How is the best way to
> > get around this?
>
> > thanks.
>
> > -patrick
>
> > document.observe("dom:loaded", function() {
> >   var test = new a;
>
> > });
>
> > var a = (function() {
>
> >         var a = Class.create({
>
> >                 initialize: function() {
> >                         new b(this.to_execute)
> >                 },
>
> >                 do_something: function() {
> >                         console.log('it has been done!');
> >                 },
>
> >                 future_func: function() {
> >                         var my_a = this;
> >                         my_a.do_something();
> >                 }
> >         });
>
> >         return a;
>
> > })();
>
> > var b = (function() {
>
> >         var b = Class.create({
>
> >                 initialize: function(func) {
> >                         func();
> >                 }
> >         });
>
> >         return b;
>
> > })();
>
>
--~--~---------~--~----~------------~-------~--~----~
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