Your syntax is fine.  If you're callilng explainYourself like this:


...it should work.  How are you calling it?  Because context (the
meaning of the "this" keyword) is slightly different in JavaScript
than in other languages using that keyword, and it's easily lost.  For

var f;
f = this.explainYourself; // Getting a reference to the function, not
calling it
f();                      // Now I'm calling it

That would fail because "this" would not be a reference to an instance
of myClass when the function was called.

Here are some resources for reading up on context:

[1] http://blog.niftysnippets.org/2008/03/mythical-methods.html
[2] http://blog.niftysnippets.org/2008/04/you-must-remember-this.html
[3] http://www.alistapart.com/articles/getoutbindingsituations

T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Aug 15, 10:27 pm, Mojito <tokyot...@gmail.com> wrote:
> I'm a complete noob to Prototype and don't know how to call a method
> from another method.
> I'm using the [URL="http://www.prototypejs.org/learn/class-
> inheritance"]Prototype JS library[/URL] to make classes.
> ++++++++++++++++++++++++
> var myClass = Class.create({
>    initialize: function() {
>       this.myValue = 5;
>    },
>    returnHello: function() {
>       return 'hello';
>    },
>    explainYourself: function() {
>       return this.returnHello() + ", my value is: " + this.myValue;
>    }});
> ++++++++++++++++++++++++++
> In explainYourself(), I tried to call returnHello() and I got an error
> saying this.returnHello() is not a function. What is the correct
> syntax?
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 
For more options, visit this group at 

Reply via email to