0

I imagine that the error below is pretty easy to fix for someone who is not a newb ( like me)

Can anyone tell me why the call to "this.slideNext() " in the code below does not work. Apparently "this.slideNext() " is not a function?

function ScoopAnimation(_path, _start, _end, _delay) {

    this.start = _start
    this.end = _end;
    this.delay = _delay;
    this.path = _path
    this.currentFrame = _start;

    this.slideNext() = function() {
        this.currentFrame++;
        console.log('  next this.currentFrame  : ' + this.currentFrame);
    }

    this.start = function() {
        console.log('next this.start()   : ' + this.currentFrame);
        //THE NEXT LINE CAUSES THE ERROR!
        this.slideNext()
    }

    this.start();

}
2
  • looks like you have your semicolons missing. can you try if the error persists when you add your semicolons? Furthermore, can you post the error message? You can see it from Web Console in Firefox, for example. Commented Feb 8, 2012 at 11:03
  • Gotcha. Semicolons weren't the problem . The error message is "this.slideNext is not a function" . Japrescott's solution removes the error. It looks like I had an extra set of paraenthesis ;) Commented Feb 9, 2012 at 9:18

4 Answers 4

1

no, that line you flaged as "the bad one" is actually correct. further up, you are trying to execute the slideNext function and then assign a function to the result. it should be this;

this.slideNext = function (){
    this.currentFrame ++;
    console.log('  next this.currentFrame  : ' +this.currentFrame );
}   

hope i helped

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

4 Comments

you weren't fast enough for a "bounty hunter", but since you're in for the points, here you go!
no, i dont care about the points, but if my answer is correct and I spent time writing it, wanting some recognition seems legit to me :) (and since you don't care about points, why not donate them to me? :P)
sry, you 're right. I'm trying not to encourage the "points race". but you're right. your answer is good => upvote
japrescott, thanks, that worked (alas, I just registered and don't have enough points to upvote. Otherwise, I would).
0

I could be wrong, but shouldn't it be defined as:

// defined without brackets
this.slideNext = function (){
    this.currentFrame ++;
    console.log('  next this.currentFrame  : ' +this.currentFrame );
    } 

Comments

0

this has a different reference/context for each function, based on how the function is invoked. In your snippet, you're calling the start function() which (called just like that) will reference the global object in its this context variable for non-ES5 strict and undefined in ES5 strict.

To solve that problem, you can either store the reference for your "outer" this in a local variable, like

var myScope = this;

and then use myScope instead of this within any other function context you need to access the outer scope.

myScope.slideNext();

Another option would be to use ES5 Function.prototype.bind to bind the context for a function. This would look like:

this.start = function() {
    console.log('next this.start()   : ' + this.currentFrame);
    //THE NEXT LINE CAUSES THE ERROR!
    this.slideNext()
}.bind(this);

Now, we bound the current value of this to the context of the start function. Now you can just continue to use this within the function. Notice that this will only work for js engines that support ES5 or you have loaded some kind of ES5 Shim script.

1 Comment

Thanks for your comment. It helped me better understand "this" in Javascript
0

If you don't intend on ScoopANimation being used as a constructor, then I'd personally ditch the use of 'this':

function ScoopAnimation(_path, _start, _end, _delay) {

  var start = _start,
      end = _end,
      delay = _delay,
      path = _path,
      currentFrame = _start;

    function slideNext() {
      currentFrame++;
      console.log('  next this.currentFrame  : ' + currentFrame);
    }

    function start() {
      console.log('next this.start()   : ' + currentFrame);
      //THE NEXT LINE CAUSES THE ERROR!
      slideNext()
    }

    start();
}

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.