Problem with prototype

G

Gerry Vandermaesen

Hi,

Consider the following code:

var Foo = Class.create();
Foo.prototype = {
initialize: function() {
this.bar = "Hello world";
},

load: function() {
new Ajax.Request("http://www.example.com/", { onSuccess: this.parse
});
},

parse: function(response) {
alert(this.bar);
}

}

foo = new Foo();
foo.load();

My problem is that although load() correctly executes the parse()
function on success, I seem to be in a different scope and all the
object's properties are undefined inside parse(). Anyone know how I
could call this function when the Ajax requests completes successfully,
while staying in the current scope?
 
G

Gerry Vandermaesen

Luckily I found the solution myself. In the "onSuccess" parameter, I
know pass "this.parse.bind(this)" and the "this" variable is bound to
the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top