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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:48:24+00:00 2026-06-09T09:48:24+00:00

I am using John Resig’s Simple JavaScript Inheritance and have run into an issue

  • 0

I am using John Resig’s Simple JavaScript Inheritance and have run into an issue where I am losing what ‘this’ refers to. Using this code:

var Runner = Class.extend({ 
 init: function() {
  this.update();
  if(!this.interval) {
   this.interval = setInterval(this.update, this.period * 1000);
  }
 },
 stop: function() {
  clearInterval(this.interval);
 },
 update: function() {
  this.success()
 },
 success: function(){
 }
});

var SubRunner = Runner.extend({
 update: function() {
  this._super();
 },
 success: function(){
  alert('sub runner success');
 }
});

Running p = new SubRunner() works as I would expect and alerts sub runner success the first time. After the first run through it then tries to run the success function on the wrong ‘this’ (window).

I know Prototype gives you a bind function so that you can pass the context to the function but I haven’t had any luck in doing something similar here. Does anyone have a starting point to figuring this out?

Thanks!

  • 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-09T09:48:26+00:00Added an answer on June 9, 2026 at 9:48 am

    The problem is when you pass this.update to the setInterval function. In Javascript, the “this” depends on wether you call the function using dot notation, and functions will not remember where they came from if you pass them as callbacks or store them in a variable.

    You can either add a wrapper function

    var that = this;
    setTimeout(function(){ that.update() }, this.perios*1000)
    

    or you can use the bind method if its available in your browser (or you can use the similar function in Prototype).

    setTimeout(this.update.bind(this), this.period*1000)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom Javascript class (created using John Resig's Simple Javascript Inheritance ).
I am using John resig's implementation class mentioned here: http://ejohn.org/blog/simple-javascript-inheritance/ Now I have a
I'm using John Resig's excellent javascript class for simple javascript inheritance and I'm having
I'm using John Resig's recipe for JavaScript 'classes' and inheritance. I've stripped my code
I decided to try out JavaScript genius John Resig's simple JavaScript inheritance as detailed
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I've been using a bit of code (by John Evans) to execute javascript from
I am using John Resig's JavaScript class definition style . Below is an example
I've been using John Resig's getStyle function from Pro JavaScript Techniques to get the
This is from John Resig`s Learning Advanced JavaScript #35 http://ejohn.org/apps/learn/#35 , called What happens

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.