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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:24:56+00:00 2026-05-25T18:24:56+00:00

Is it possible to inherit privileged methods in Javascript? In the below, Widget successfully

  • 0

Is it possible to inherit privileged methods in Javascript? In the below, Widget successfully inherits the D function, but not subscribe. Changing the second line in inherit to f.prototype = new base(); seems to work, but I know that’s bad for all sorts of reasons. Is there a clean way to do this, or do I have to make everything public? This answer seems to imply that I have to make the methods public (attach to prototype) but I’d like to ask directly.

    function EventRaiser() {
        var events = {};
        this.subscribe = function(key, func) { /* elided */ };
    }
    EventRaiser.prototype.D = function() { alert("D"); };

    function Widget() { }

    function Inherit(sub, base) {
        function f() { }
        f.prototype = base.prototype;
        sub.prototype = new f();
        sub.prototype.constructor = sub;
    }

    Inherit(Widget, EventRaiser);
  • 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-25T18:24:57+00:00Added an answer on May 25, 2026 at 6:24 pm

    this.subscribe = function(key, func) { /* elided */ };

    here your adding a method to the current thisContext.

    Inherit(Widget, EventRaiser)

    Here your saling the prototype Widget should consume the prototype EventRaiser.

    The best you can do is to not mix this.x with prototype.y

    Or you can call EventRaiser.call(this) inside function Widget() { } but that’s bad style.

    If your going to use an inheritance pattern I would recommend you use Object.create & pd :

    // prototype
    var EventRaiser = {
        D: function() { alert("D"); },
        subscribe: function(key, func) { ... }
    };
    // factory
    var eventRaiser = function _eventRaiser(proto) {
        proto = proto || EventRaiser;
        return Object.create(proto, pd({
            events: {}
        }));  
    };
    
    // prototype
    var Widget = {
        ...    
    };
    // factory
    var widget = function _widget() {
        var o = eventRaiser(pd.merge(Widget, EventRaiser));
        Object.defineProperties(o, pd({
            ... 
        });
        return o;
    };
    

    Or if you insist Widget should inherit from EventRaiser

    var Widget = Object.create(EventRaiser, pd({
        ...
    });
    var widget = function _widget() {
        var o = eventRaiser(Widget);
        Object.defineProperties(o, pd({
            ...
        });
        return o;
    }
    

    The reasons for recommending this pattern is a clear seperation of the prototype and the factory. This allows you to interact with the prototype without handling the factory. With the use of the new keyword you muddy those waters (as shown in your code) and you also tend to hack this around.

    The above code also doesn’t look elegant. This means that your realy aught to look for a different pattern. For example EventEmitter from node.js has an explicit check for the this._events object which makes the factory code more elegant.

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

Sidebar

Related Questions

I know it's not possible to inherit constructors in C#, but there's probably a
Is it possible to subclass and inherit from javascript Arrays? I'd like to have
It's not possible to inherit from a C# struct. It's not obvious to me
I know it isn't possible to inherit from a generic type parameter, but it
Possible Duplicates: Why does Java allow multiple inheritance from interfaces but not from abstract/concrete
It is not possible to inherit from System.Delegate or System.MulticastDelegate in C#. It is
is it possible to inherit an Object (reference) value from a class? function A()
I'd like to inherit records, since this is not possible the best solution is
Is it possible to inherit from both ViewPage and ViewPage<T>?? Or do I have
I have only a basic knowledge of css, is it possible to inherit a

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.