3
var sc = new stuCore();
function stuCore() {
    this.readyPages = [];
    this.once = true;
    var self = this;

    // gets called asynchronously
    this.doPrepPage = function (page){

            if(self.once == true){
                   // still gets executed every time, assignment fails
                   self.once = false;
                   doSomeStuffOnce();
            }
    };

   this.addReadyPage = function (pageid) {

            console.log("readypage called");
            this.readyPages.push(pageid);
            if (!$.inArray(pageid, self.readyPages) != -1) {
                    this.doPrepPage(pageid);
            }
    };


}

why does this assignment fail? I thought I knew the basics of js, but I'm stumped by this. And furthermore what would be a possible solution? call a constructor first and set the variable there?

EDIT: gets called like this in some other script:

  sc.addReadyPage(self.id);
6
  • 1
    // gets called asynchronously How exactly are you calling it? How are you calling stuCore? It works fine for me: jsfiddle.net/fkling/KwRKf Commented Aug 3, 2012 at 9:53
  • where are you checking the value of self.once? and is the function executed more than once? Commented Aug 3, 2012 at 9:54
  • Did you try using a debugger? What is this at the moment you assign it to self? Commented Aug 3, 2012 at 9:54
  • Provide more context: the value of this depends on where you are calling this code from (and/or if you are using constructor syntax with the 'new' operator). If you call stuCore() from two different contexts, the doSomeStuffOnce() will be called twice... Commented Aug 3, 2012 at 9:56
  • 1
    Sorry, but the code you posted does not have the problem you describe... have a look at my demo. Or your description is not very accurate. You will be able to call doPrepPage at least once for every instance you create. Either you provide one yourself, which shows the problem, or we cannot help you. Commented Aug 3, 2012 at 10:04

2 Answers 2

1

The jQuery.inArray function will return the index in the containing array for the given value. Your script pushes pageid into this.readyPages before checking whether it exists in self.readyPages. this.readyPages and self.readyPages are the same array reference, so the result will always be zero or greater, so the condition that calls doPrepPage will never run.

You could try switching their order around:

this.addReadyPage = function (pageid) {
        console.log("readypage called");
        if ($.inArray(pageid, self.readyPages) != -1) {
                this.readyPages.push(pageid);
                this.doPrepPage(pageid);
        }
};

(edit: Removed the additional !, thanks @chumkiu)

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly you're calling this.doPrepPage as <insert variable name here>.doPrepPage?

If this is the case then your var self passes through to the anonymous function and is stored there, so everytime you call this.doPrepPage it takes the local variable of self.

Try setting self to a global variable, this way it will permanently modify self so each time this.doPrepPage is called it uses the updated variable.

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.