1

I am experiencing a funny issue with JavaScript. I have a feeling this could have something to do with Closures, which, I admit, I am not really comfortable with.

Problem It seems that calling a function from within results in a premature termination of the loop. I tried running the same loop without the function call and console.log() outputs the counter accurately suggesting the function call is breaking the loop.

Could someone suggest a possible fix? I have the code pasted here:

AbstractModel.prototype.deactivateContext   = function(context){

  for(i=0;i<this.asset.length;i++){
      if(this.asset[i].context == context){
        this.asset[i].deactivate();
        console.log(i);
        this.notify(this.asset[i],"REFRESHASSETS");
      }
  }
}
8
  • 1
    Where did you call this method? (Did you call the method?) This is just a method definition; it won't actually run the body. Commented May 2, 2014 at 13:35
  • 3
    Which particular function call seems to be the problem? There are 3 in that loop. Commented May 2, 2014 at 13:37
  • AbstractModel will have a method called deactivateContext. Looks like you have Assets object with method called deactivate. If you show us the invocations and more code, we will be able to comment better Commented May 2, 2014 at 13:38
  • 3
    Oh, I found it. i is not declared. i is coming from global context. Commented May 2, 2014 at 13:39
  • 2
    You forgot to declare "i" with var. If the other function also uses "i" to control a loop, then you'll have problems. Commented May 2, 2014 at 13:39

3 Answers 3

5

var is not optional

You proabbly have a global i in the other method. Declare your variables to the correct scope.

for (var i=0; i<this.asset.length; i++) {
     ^^^
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, the issue is probably a side effect altering either "i" or this.asset.length
I am using 'i' in the other function (notify) as well without explicitly declaring it there which would explain why the loop ends early. Cheers!
3

Please declare i in the following function, like

AbstractModel.prototype.deactivateContext   = function(context){

  for(var i=0;i<this.asset.length;i++){
      if(this.asset[i].context == context){
        this.asset[i].deactivate();
        console.log(i);
        this.notify(this.asset[i],"REFRESHASSETS");
      }
  }
}

Looks like i is getting modified to a higher number in the notify function, when it returns from notify the for check is exiting.

Comments

1

Have you tried wrapping the call to the notify function in try catch. If you are saying that when you remove the call, output of counter prints as expected then the only explanation would be that your function is throwing an exception. Try this:

 for(i=0;i<this.asset.length;i++){
      if(this.asset[i].context == context){
        this.asset[i].deactivate();
        console.log(i);
        try {
          this.notify(this.asset[i],"REFRESHASSETS");
        } catch(e) {
           alert(e);
           console.log(e);
        }
      }
  }

2 Comments

+1, that's a really smart debugging tip but the issue was resolved by declaring the counter explicitly. I am using 'i' in the other functino (notify) as well without explicitly declaring it which would explain why the loop ends early. Thanks!
oh cool, sorry did notice that you are not declaring var explicitly.

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.