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 7781045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:05:55+00:00 2026-06-01T19:05:55+00:00

I have a JavaScript class function MyClass() { MyClass.prototype.fooMethod = function () { //

  • 0

I have a JavaScript class

function MyClass() {

  MyClass.prototype.fooMethod = function () {
    // some logic goes here...
  }

  MyClass.prototype.fooMethod2 = function () {
    this.fooMethod();
  }
}

Everything works as expected when I call like this:

var m = MyClass();
m.fooMethod2();

However when I have this code called by setInterval I get an error: “Uncaught TypeError: Object [object DOMWindow] has no method ‘fooMethod'”

var m = MyClass();
var intervalId = setInterval(m.fooMethod2, 100);

Is there any way I can use setInverval to call my method?

  • 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-06-01T19:05:56+00:00Added an answer on June 1, 2026 at 7:05 pm
    setInterval(function() {
      m.fooMethod2();
    }, 100);
    

    In javascript, context (the value of this) is set by how the method is invoked. So foo.bar() will set this to foo inside the bar function.

    But when you return a a reference to a function to be run by something else, context is not preserved. Check this out:

    var callLater = function(fn) {
      setTimeout(function() {
        fn(); // no defined context, `this` will revert to the default: `window` 
      }, 1000);
    };
    callLater(foo.bar);
    

    In this snippet, we pass a function into the callLater function. But when we invoke fn() we do it without any receiver, meaning we lose context.

    So anytime function context is important, you have to pass in an anonymous function that will call the method on an object explicitly.

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

Sidebar

Related Questions

I have some javascript that goes out and fetches a javascript class on another
I have some HTML that looks like this: <div class=MyClass></div> <div class=MyClass></div> <div class=MyClass></div>
I have this JavaScript prototype: Utils.MyClass1 = function(id, member) { this.id = id; this.member
I have a javascript class defined like this: myClass = { var1:0, var2:0, initVars:
I have a javascript class like this: function Foo() { this.x = 5; this.y
I have in my javascript these 2 functions classes: // product class function Product()
Lets say I have this: <object class=MyClass type=text/html data=/Whatever/1?renderpartial=1></object> <object class=MyClass type=text/html data=/Whatever/2?renderpartial=1></object> And,
I have a javascript function (class) that takes a function reference as one paremter.
I have following javascript class: var ImageGallery = function(arr) { var ImgArr = arr;
Say I have a javascript function/class called Foo and it has a property called

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.