470,624 Members | 2,472 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

Problem with prototype

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?

Oct 30 '06 #1
1 1105
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.
Gerry Vandermaesen wrote:
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?
Oct 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Yereth Jansen | last post: by
39 posts views Thread by Martin JÝrgensen | last post: by
12 posts views Thread by Assimalyst | last post: by
1 post views Thread by mikejr83 | last post: by
2 posts views Thread by allen.leis | last post: by
11 posts views Thread by jessy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.