Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 745315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:04:50+00:00 2026-05-14T09:04:50+00:00

I’m trying to create an image rotator class that cycles through an arbitrary number

  • 0

I’m trying to create an image rotator class that cycles through an arbitrary number of images in an unordered list. Is it possible to define a recursive function within a class declaration? E.g:

var Rotator = Class.create() {
 initialize: function() {
  do something...

  this.rotate();
 }

 rotate: function() {
   do something...

   this.rotate()
 }
}

It currently throws an error stating “this.rotate() is not a function”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:04:50+00:00Added an answer on May 14, 2026 at 9:04 am

    Answer:

    What you have there should work (except for a couple of what I think are typos; below), because you’re accessing the function from the property. Note that that means you’ll re-enter the top-most (most sub-classed) rotate function if you’re using inheritance, because that’s the one that’s assigned to the instance rotate property.

    You said that it’s saying that this.rotate is not a function. How are you calling it? Because if you’re doing something like this:

    var r = new Rotator();
    setInterval(r.rotate, 1000);
    

    or (within rotate):

    setInterval(this.rotate, 1000);
    

    …that’s not going to work, because you’re just passing the function (not its context) to setInterval. This would work:

    var r = new Rotator();
    setInterval(r.rotate.bind(r), 1000);
    

    or (within rotate):

    setInterval(this.rotate.bind(this), 1000);
    

    That uses Function#bind to create a function that will set the correct context. More about functions vs. methods in Javascript in this post.

    Typos:

    var Rotator = Class.create() {
    ...
    }
    

    should be

    var Rotator = Class.create({
    ...
    });
    

    You also need a comma between the two functions in the object literal notation you’re using. So with those cleaned up it’s:

    var Rotator = Class.create({ // <= open brace *within* the parens
    
        initialize: function() {
            // do something...
    
            this.rotate();
        },                       // <= missing comma was here
    
        rotate: function() {
            // do something...
    
            this.rotate();
        }
    
        return pubs;
    });                          // <= close the braces and parens here
    

    Named functions (and typo-avoidance, and private functions):

    FWIW, you can avoid typos like leaving out the comma between functions and also get all the benefits of your functions having real names (as opposed to being anonymous functions bound to properties) plus getting private class-wide functions (if that’s useful to you). This is the idiom I mostly use (although I use a helper to make the syntax a small bit cleaner; the below is raw Prototype):

    var Rotator = Class.create((function(){
        var pubs = {};
    
        pubs.initialize = initialize;
        function initialize() {
            // do something...
    
            this.rotate();
        }
    
        pubs.rotate = rotate;
        function rotate() {
            // do something...
    
            this.rotate();
        }
    
        return pubs;
    })());
    

    More on that idiom (and why you can’t combine the pubs and function links above) in the linked post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.