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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:04:04+00:00 2026-06-12T16:04:04+00:00

I have some javascript using the Revealing Module Pattern as shown below. It accepts

  • 0

I have some javascript using the Revealing Module Pattern as shown below. It accepts a callback function that it later invokes. I want that callback function to then be able to call functions defined in the class, but it’s not working.

window.MyClass = function() {
  var self = this,

  start = function (callback) {
    callback(self);
  },

  cancel = function() {
    console.log('Cancel invoked');
  };

  return {
    start: start,
    cancel: cancel
  };
};

var myCallbackFunction = function(instance) {
  instance.cancel(); // Error: instance.cancel is not a function
};

var obj = new window.MyClass();
obj.start(myCallbackFunction);

I can rework this sample into the Revealing Prototype Pattern and it works as expected, so my question is can I get this working using RMP, or is it just a limitation of this pattern?

Thanks,
Roger

  • 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-12T16:04:06+00:00Added an answer on June 12, 2026 at 4:04 pm
    window.MyClass = function() {
      var self = this;
    
      this.start = function (callback) {
        callback(self);
      };
    
      this.cancel = function() {
        console.log('Cancel invoked');
      };
    
      return {
        start: this.start,
        cancel: this.cancel
      };
    };
    

    Then, it’s both an instance method, and available via the returned value.

    But for something this simple (e.g. no private variables), I would use a straight-up prototype (“Prototype Pattern”).

    By the way, if you’re reading this series, you should be aware these are not really standard terms.

    He also stated, “Although JavaScript isn’t designed with the concept of classes or object oriented programming in mind as with C# or Java, with a little work you can achieve similar results.” In fact, JavaScript is designed for object-oriented programming, but it’s prototype-based. It’s also flexible enough to allow alternatives, as you’ve seen; those alternatives are definitely better in some cases.

    You don’t even need self, since your objects stay bound.

    So:

    window.MyClass = function(){};
    
    window.MyClass.prototype = {
        start: function (callback) {
            callback(this);
        },
    
        cancel: function () {
            console.log('Cancel invoked');
        }
    };
    
    
    var myCallbackFunction = function(instance) {
      instance.cancel();
    };
    
    var obj = new window.MyClass();
    obj.start(myCallbackFunction);
    
    • 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 I am using for some audio on my website:
Using Apache Wicket 1.4: I have a panel that calls some setup Javascript in
I have some javascript that I'm trying to retool using jQuery to learn the
Have some JavaScript that assigns values to an object using $.data and then uses
I'm using RSpec/Capybara as my test suite. I have some javascript that dynamically appends
I have some javascript array that I append within a function and call after
I'm trying to filter some XML in JavaScript using E4X and have some specific
I have some post build event to minify my css and javascript using ajaxmin
I have been using some Javascript to create a text field once an certain
I've been using Firebug to debug some javascript I have on one of my

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.