Hi, > myMethod(). I suspect this has something to do with the "this" scope > in my anonymous function.
It's true that `this` within the function will not be what it was outside the function unless you do something to make it so, and certainly that would need to be fixed if you were going to do this this way. But you seem to be assuming that the window load event has not already happened when your `initialize` function is called. When are you calling it? I'd suggest it would be better to call your `initialize` function (by creating an instance via `new`) *after* the load has occurred (from some kind of load event handler [window.load or dom:loaded]), rather than writing the class code such that it assumes it will be called before the load is complete. The latter is delicate; the class won't work correctly if the page has already been loaded. * * * * // Where you're defining your class var MyClass = Class.create({ initialize: function() { this.myField = new Field(); }, myMethod: function() { alert(this.myField); } }); // You probably have some main "load" function somewhere document.observe('dom:loaded', function() { someAppropriateVarHere = new MyClass(); }); * * * * Re your request for bind coding help in your follow-up, there are lots of examples out there if you look. Some places to look: http://prototypejs.org/api/function/bind http://proto-scripty.wikidot.com/prototype:tip-using-an-instance-method-as-a-callback-or-event-handler HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Aug 22, 10:06 pm, Mojito <tokyot...@gmail.com> wrote: > MyClass = Class.create({ > initialize: function() { > this.myField = null; > Event.observe(window, 'load', function() { > this.myField = new Field(); > }); > }, > myMethod: function() { > alert(this.myField); > } > > }); > > +++++++++++++++++++++++++++++++ > > Field's constructor uses Scriptaculous's sliders, which require an > element to be already loaded. That's why I'm only setting > this.myField when the window has loaded. I see that my sliders get > initialized properly, but this.myField is still null when I call > myMethod(). I suspect this has something to do with the "this" scope > in my anonymous function. How do I fix this to do what I intended? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---