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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:13:29+00:00 2026-05-14T00:13:29+00:00

I need to extend a class, which is encapsulated in a closure. This base

  • 0

I need to extend a class, which is encapsulated in a closure. This base class is following:

var PageController = (function(){

    // private static variable
    var _current_view;

return function(request, new_view) {
    ...

    // priveleged public function, which has access to the _current_view 
    this.execute = function() {
        alert("PageController::execute");
    }

}
})();

Inheritance is realised using the following function:

function extend(subClass, superClass){
var F = function(){
};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
StartController.cache = '';


if (superClass.prototype.constructor == Object.prototype.constructor) {
    superClass.prototype.constructor = superClass;
}
}

I subclass the PageController:

var StartController = function(request){
    // calling the constructor of the super class
    StartController.superclass.constructor.call(this, request, 'start-view');
}
// extending the objects
extend(StartController, PageController);

// overriding the PageController::execute
StartController.prototype.execute = function() {
alert('StartController::execute');
}

Inheritance is working. I can call every PageController’s method from StartController’s instance. However, method overriding doesn’t work:

var startCont = new StartController();
startCont.execute();

alerts “PageController::execute”.
How should I override this 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-05-14T00:13:29+00:00Added an answer on May 14, 2026 at 12:13 am

    It doesn’t work because StartController calls PageController which adds an execute property to your object, so the execute property of StartController.prototype is not used.

    For your overriding to work, you have to either :

    1) define PageController.prototype.execute as the execute method of PageController. It won’t work because then the function doesn’t have access to _current_view.

    2) define StartController.execute in the object constructor :

    var StartController = function(request){
        // calling the constructor of the super class
        StartController.superclass.constructor.call(this, request, 'start-view');
        // overriding the PageController::execute
        this.execute = function() {
          alert('StartController::execute');
        }
    }
    // extending the objects
    extend(StartController, PageController);
    

    edit:

    So you want for StartController.execute to access _current_view, which is impossible as long as _current_view is part of a closure that StartController is not part of. You might have to proceed like this:

    (function () {
      var _current_view;
      window.PageController = function(request, new_view) {
       ...
       this.execute = function() { ... }
      }
    
      window.StartController = function(request) {
        StartController.superclass.constructor.call(this, request, 'start-view');
        this.execute = function() { ... }
      }
      extend(StartController, PageController);
    
    }()
    var startCont = new StartController();
    startCont.execute();
    

    And if you want some kind of protected behavior, you might want to try this trick:

    (function() {
      var token = {};
    
     window.Class1 = function() {
        this.protectedMethod = function(tok) {
          if(tok != token) return; // unauthorized
          ...
        }
      }
    
      window.Class2 = function() {
        new Class1().protectedMethod(token); // access granted
      }
    })()
    
    new Class1().protectedMethod(); // access denied
    

    There’s no such thing as a package in javascript, so your possibilities are limited. You can certainly not have any kind of privileges among functions/objects/constructors that are not part of the same script. None that I know of, at least. Except maybe querying a server for some kind of authorization.

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

Sidebar

Related Questions

I need to solve the following question which i can't get to work by
I need to determine if a Class object representing an interface extends another interface,
Need a function that takes a character as a parameter and returns true if
Need to locate the following pattern: The letter I followed by a space then
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
i have a input tag which is non editable, but some times i need
Need a way to allow sorting except for last item with in a list.
Need to an expression that returns only things with an I followed by either
need ask you about some help. I have web app running in Net 2.0.
I need to know about Epoll On linux System. Could you recommend manual or

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.