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

  • Home
  • SEARCH
  • 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 8213781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:14:14+00:00 2026-06-07T11:14:14+00:00

I created a JavaScript class, and I would like to call a method inside

  • 0

I created a JavaScript class, and I would like to call a method inside every second, here is my code:

var MyClass = Base.extend({
    myMethod: function() {
        setTimeout(function(){
            var mc = new MyClass();
            mc.myMethod();
        }, 1000);
    }
});
var myGlobalClass = new MyClass();

myGlobalClass.myMethod();

Actually this code works, but I need to do a new MyClass() every time. I would had liked using myGlobalClass.

Is this possible ?

Just a precision this.myMethod() can’t work in this case, if you thought of that.

  • 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-07T11:14:15+00:00Added an answer on June 7, 2026 at 11:14 am

    If you want to reffer to the value myGlobalClass then you can just do so directly from the function

    setTimeout(function(){
      myGlobalClass.myMethod();
    }, 1000);
    

    This works because Javascript doesn’t just look for locals defined in the current scope when resolving names. It looks at all of the parent scopes as well to resolve them. In this case myGlobalClass is defined in the global scope. Name resolution begins in the setTimeout function callback and it won’t find it in the function, it will then search myMethod: function() { and won’t find it there, it will then look in the global scope and discover myGlobalClass. It will resolve the name to this value

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

Sidebar

Related Questions

I created a javascript class as follow: var MyClass = (function() { function myprivate(param)
I have created a Queue class in javascript and I would like to store
Let's say you have a JavaScript class like this var DepartmentFactory = function(data) {
Hi I would like to have an explanation regarding the following code. Here Sometimes
I would like to create a Javascript class that I can use like so:
The code below uses Javascript to create a base class, eventRaiser , that has
I want to dynamically call the method of a custom class much like the
I want to create a Javascript class/object that allow me to have various method:
for example: I have created one javascript property array model with some properties like
I am trying to organize my JavaScript. What I would like to do is

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.