473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1178
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
1
by: mikejr83 | last post by:
I've been working on a project converting some IE code to be cross-browser complient. I've run into a little problem figuring out how to migrate the IE specific mergeAttributes function to...
6
by: Sree | last post by:
If the program (myprog) is run from the command line as myprog 1 2 3 , What would be the output? main(int argc, char *argv) { int i; for(i=0;i<argc;i++) printf("%s",argv); }
2
by: allen.leis | last post by:
Hey all - I was making a few objects using the prototype.js library and ran into a problem. Specifically I'm trying to create a Squadron class which contains an array of Platform class. All...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
5
RamananKalirajan
by: RamananKalirajan | last post by:
Hi those who were working in Ajax, will surely experienced with this problem. SOP - Single Origin Policy problem. I am doing a small work in Prototype Ajax. I did a sample work. When I am passing the...
11
by: jessy | last post by:
Hi, I have a problem with my DateTimePicker javascript code which i downloaded , the problem is when i pick the date and the date appears in my Text Field and i click Submit the date which i picked...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.