0

When I start my script I have this:

var my_great_masterpiece = new function ()
{
        var self = this;

Then later in my script I have this:

        response_xml: function ()
        {
            if (self.http_request.readyState == 4)
            {
                if (self.http_request.status == 404 && countXmlUrl <= 3)
                {
                    countXmlUrl++;
                    self.realXmlUrl = xmlUrl[countXmlUrl];
                    self.request_xml();
                }
                if (self.http_request.status == 200)
                {
                    self.xmlDoc = self.http_request.responseXML;
                    self.storage.setItem('domains_raw_xml', self.http_request.responseText);
                    self.main.peter_save_data();
                    self.timervar = setTimeout(function ()
                    {
// ########### Below line gives the error #############################
                        self.new_version_show_window();
                    }, 2000);
                }
            }
        },
        new_version_show_window: function ()
        {
...
}

the error that I am getting is:

Error: self.new_version_show_window is not a function

What am I doing wrong?

Thanks!

5
  • Have you tried moving the function definition for new_version_show_window above where it is called? Also, use a semicolon instead of a comma before new_version_show_window Commented Jun 23, 2011 at 18:15
  • How is the second part connected to the first part? Do you set the prototype? Commented Jun 23, 2011 at 18:16
  • @josh.trow: No, it seems to be an object literal. Properties are separated by comma. Commented Jun 23, 2011 at 18:17
  • @josh.trow - You are misunderstanding the code. There are no syntax errors that I can see. Commented Jun 23, 2011 at 18:19
  • You have to provide more information. We have to know where new_version_show_window is defined. Right now it could be everywhere... Commented Jun 23, 2011 at 18:22

3 Answers 3

5

It is unclear from your code where new_version_show_window is defined. Maybe you could explicitly define it on self:

self.new_version_show_window = function () {
    /* ... */
}

instead. Or you could define it in the local namespace and use it directly in the setTimeout call:

self.timervar = setTimeout(function () {
    new_version_show_window();
 }, 2000);

or simply:

self.timervar = setTimeout(new_version_show_window, 2000);

Because of closure, the variables declared in the outer function is also available in the inner function.

Edit

Thanks for posting the entire code. new_version_show_window is defined on this.main, so you must access it thusly:

self.timervar = setTimeout(function () {
    self.main.new_version_show_window();
 }, 2000);
Sign up to request clarification or add additional context in comments.

14 Comments

If the (partial) object literal is used as prototype, then it should work. But we need more information...
Yes, you're right about that. However, since self closes over all the functions, it is basically a singleton - making little sense for a prototype.
@Ryan: As I said, we actually need the complete code. There is no way to tell what you did wrong because we don't know in which scope the second part is.
new_version_show_window is defined on this.main, so you have to call self.main.new_version_show_window();
That worked! Thanks guys! Don't know when JS got this complicated... I used to work with some simple stuff a couple of years back and am just getting back into it. Anonymous functions... encapsulated objects... never did/had that before and am quite lost. Would be in deep poop if it was not for SO and you wonderful people... thanks!
|
0

It could be that self is a reserved word in JavaScript [1]. This could be causing you some problems so try naming the variable something different to start with.

[1] http://www.quackit.com/javascript/javascript_reserved_words.cfm

3 Comments

@Jivings: Well, I can tell you that there is no problem using it in a browser context. Otherwise a lot of my code would be broken... It is definitely possible to define it as local variable in a function. There are no other predefined variables with which it could clash.
@Felix - Haha okay, tbh I think I've used it before also. I just didn't see anything else immediately wrong with the code.
Felix is right. It may be on those lists, but self causes no problems as a local variable at least. I've used it plenty without any issues.
0

This is a problem of scope. new_version_show_window is only in scope in the construct in which is it called ( apparently a jQuery AJAX function of some sort). It will only be available to my_great_masterpiece if you define it outside the limited scope in which it now exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.