Problem with object

N

noon

var pi_XHR = {

init: function init(title, d) {
pi_Logger.log('The item title is "' + title + '"');
title = title.replace(/\s+/g, '+');
this.request('http://imdb.com/find?s=tt&q='+title, this.decide,
d);
},

request: function request(url, callback, d) {
pi_Logger.log('Requesting ' + url + ' with a callback of "' +
callback.name + '()"');
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = this.rs(xhr, url, callback, d);
xhr.setRequestHeader('Referer', 'http://google.com');
xhr.send(null);
},

rs: function rs(xhr, url, callback, d) {
return function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) { pi_Logger.log('HTTP '+xhr.status);
return; }
with (xhr.responseText) {
if (indexOf('<title>IMDb Title') != -1) {
pi_Logger.log('We got IMDB search results');

var s = indexOf('<br>1.</td>');
if (s == -1) {
pi_Logger.log('There werent any results on this page');
return;
}

var link = substring(s, indexOf('</a>', s));
link = link.substring(link.indexOf('<a href="')+9,
link.lastIndexOf('">'));
window.setTimeout(this.request, 1, link, callback, d);
pi_Logger.log('Extracted ' + link);
}
else if (indexOf('A message from today') != -1) { // RT ad page
pi_Logger.log('RT ad page...');
} else {
pi_Logger.log('We think we have good HTML and are sending it to
"' + callback.name + '()"');
callback(xhr.responseText, d);
}
}
}
},

decide: function decide(html, d) {

pi_Logger.log("decide::Type of 'this' = "+typeof(this));
pi_Logger.log("decide::Name of 'this' = "+this.name);
pi_Logger.log("decide::Type of 'this.request' = "
+typeof(this.request));
}
};

Everything is good and fine until I get to my decide function. For
whatever reason this.request doesn't exist. Console shows me that the
type of this is object, name of this is blank, and
typeof(this.request) is undefined. If I replace this with pi_XHR it
works fine again. But in my other functions I can use this...

Critiquing, *cough* making fun of *cough*, my weak code skills is
encouraged
 
R

RobG

var pi_XHR = {

        init: function init(title, d) {
                pi_Logger.log('The item title is "' +title + '"');
                title = title.replace(/\s+/g, '+');
                this.request('http://imdb.com/find?s=tt&q='+title, this.decide,
d);

It is better to use say 2 or 4 spaces for indenting posted code, and
wrap it at about 70 characters so it doesn't auto-wrap.

When you call pi_XHR.init and then this.request, this is a reference
to pi_XHR so this.decide should work as expected.
        },

        request: function request(url, callback, d) {
                pi_Logger.log('Requesting ' + url + ' witha callback of "' +
callback.name + '()"');
                var xhr = new XMLHttpRequest();
                xhr.open("GET", url, true);
                xhr.onreadystatechange = this.rs(xhr, url, callback, d);
                xhr.setRequestHeader('Referer', 'http://google.com');
                xhr.send(null);
        },

        rs: function rs(xhr, url, callback, d) {
                return function() {
                        if (xhr.readyState != 4)return;
                        if (xhr.status != 200) {pi_Logger.log('HTTP '+xhr.status);
return; }
                        with (xhr.responseText) {

Using with is generally considered bad form as it screws with the
scope chain.

                                if (indexOf('<title>IMDb Title') != -1) {
                                        pi_Logger.log('We got IMDB search results');

                                        var s = indexOf('<br>1.</td>');
                                        if (s == -1) {
                                                pi_Logger.log('There werent any results on this page');
                                                return;
                                        }

                                        var link = substring(s, indexOf('</a>', s));
                                        link = link.substring(link.indexOf('<a href="')+9,
link.lastIndexOf('">'));
                                        window.setTimeout(this.request, 1, link,

When you use window.setTimeout, the function passed to it will run in
a global scope. You pass 'this.request' which is a refence directly
to request, so when request is called its this keyword points to the
global object, so you are trying to call window.decide, not
pi_XHR.decide.

Using fully qualified path, pi_XHR.decide(), fixes that, or you could
try using a closure with the value you want:

var thisRequest = this.request;
window.setTimeout(thisRequest, ...)


Untested of course!
 
T

Thomas 'PointedEars' Lahn

RobG said:
[...]
window.setTimeout(this.request, 1, link,

When you use window.setTimeout, the function passed to it will run in
a global scope. You pass 'this.request' which is a refence directly
to request, so when request is called its this keyword points to the
global object, so you are trying to call window.decide, not
pi_XHR.decide.

Using fully qualified path, pi_XHR.decide(), fixes that, or you could
try using a closure with the value you want:

var thisRequest = this.request;
window.setTimeout(thisRequest, ...)

Untested of course!

There is *no* closure here which is why the method loses the connection to
its owner as well when called so. You were looking for this instead:

var me = this;
window.setTimeout(
function()
{
me.request();
},
...
);

Another possibility that I like better, since it limits the scope of the
variable:

window.setTimeout(
(
function(me)
{
return function()
{
me.request();
};
}
)(this),
...
);


PointedEars
 
S

sasuke

var pi_XHR = {

init: function init(title, d) {

You don't need the function name there, you might have as well
written:

var obj = {
func: function(a) {
alert("A useless alert");
}
};

Sorry for being picky, just thought would let you know just in case
you didn't know that already.
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top