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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:54:37+00:00 2026-05-25T20:54:37+00:00

The code below uses Javascript to create a base class, eventRaiser , that has

  • 0

The code below uses Javascript to create a base class, eventRaiser, that has the internals needed to allow clients to subscribe to events, and subclasses to raise these events. The idea is that other classes, like ThingWithEvent, will inherit from eventRaiser and expose the subscribe method, and fire off the raise method internally. The jQuery init function demonstrates this.

The way this is written, there’s nothing stopping a client from directly raising an event. In other words, adding er.raise("Y"); to the jQuery init function causes the Y event to be raised without difficulty.

Ideally I’d like to make it so that outside code, interacting with eventRaiser through some class that inherits from it, with can only subscribe to events, and not raise them.

In other words I’d like raise to be the equivalent of C# protected—visible only to itself, and classes that inherit from it.

Is there some slick ninja closure I should use to achieve this, or should I recognize that Javascript is not meant to incorporate OO Encapulation, rename raise to _raise to imply to client code that _raise is private and should not be invoked, and move on?

    $(function() {
        var er = new ThingWithEvent();

        er.subscribe("X", function() { alert("Hello"); });
        er.subscribe("X", function() { alert("World"); });
        er.subscribe("Y", function() { alert("Not Called"); });

        er.doSomething("X");
    });

    function eventRaiser() {
        var events = {};
        this.subscribe = function(key, func) {
            if (!events[key])
                events[key] = { name: key, funcs: [] };
            events[key].funcs.push(func);
        };

        this.raise = function(key) {
            if (!events[key]) return;
            for (var i = 0; i < events[key].funcs.length; i++)
                events[key].funcs[i]();
        };
    }

    function ThingWithEvent() {
        eventRaiser.call(this);
        var self = this;

        this.doSomething = function() {
            alert("doing something");
            self.raise("X");
        }
    }

    function surrogate() { }
    surrogate.prototype = eventRaiser;
    ThingWithEvent.prototype = new surrogate();
    ThingWithEvent.prototype.constructor = ThingWithEvent;
  • 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-25T20:54:38+00:00Added an answer on May 25, 2026 at 8:54 pm

    I hate answering my own question, let alone accepting my own answer, but it turns out this is not only possible, but insanely easy. The idea behind this code comes from Douglas Crockford’s JavaScript The Good Parts. The trick is to ditch constructors (neoclassical inheritance) altogether and use functional inheritance.

    This code not only achieves public, protected, and private access levels, but it’s much cleaner, and easier to read IMO than inheriting constructors and swapping prototypes and constructors around. The only catch is that this code will be slightly slower since each object creation necessitates the creation of each of the object’s functions, instead of getting all that dumped in for free with a constructor’s prototype. So, if you need to create tens of thousands of objects in your web site then you might prefer a constructor. For…everyone else, this code is likely for you.

        function eventRaiser(protectedStuff) {
            protectedStuff = protectedStuff || {}; 
            var that = {};
            var events = {};  //private
    
            protectedStuff.raise = function(key) {
                if (!events[key]) return;
                    for (var i = 0; i < events[key].funcs.length; i++)
                        events[key].funcs[i].apply(null, Array.prototype.slice.call(arguments, 1));
            };
    
            that.subscribe = function(key, func) {
                if (!events[key])
                    events[key] = { name: key, funcs: [] };
                events[key].funcs.push(func);
            };
    
            return that;
        }        
    
        function widget() {
            var protectedStuff = {};
            var that = eventRaiser(protectedStuff);
    
            that.doSomething = function() { 
                alert("doing something"); 
                protectedStuff.raise("doStuffEvent");
            };
    
            return that;
        }
    
        $(function() {
            var w = widget();
            w.subscribe("doStuffEvent", function(){ alert("I've been raised"); });
            w.doSomething();
    
            w.protectedStuff.raise("doStuffEvent"); //error!!!!!  raise is protected
            w.raise("doStuffEvent"); //and this obviously won't work
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this javascript code below that uses jquery, it is suppoed to be
I've some simple code below that uses a ToggleButton.IsChecked property to set the Visibility
The below code which uses npoi to create excel documents displays images in open
I am maintaining some code someone has created and it uses a few JavaScript
The code below shows a sample that I've used recently to explain the different
I have a website that is 1 html file and uses javascript to hide
I have the following code which uses JavaScript to select a text box when
I have the code that I have posted below in my webpage. As you
I've inherited a site that uses Google's GSNewsBar . The wizard creates code that
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {

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.