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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:21:42+00:00 2026-06-13T09:21:42+00:00

I got a RequireJs module which instances another module and proxies some methods of

  • 0

I got a RequireJs module which instances another module and proxies some methods of it. I would now like to hide the module instance itself and only allow access through the proxy-methods.

define(['mediator'], function(Mediator) {

  var Proxy;

  Proxy = function(prefix) {
    this.prefix = prefix;
    this.mediator = new Mediator();
  };

  Proxy.prototype.on = function(event, callback, context) {
    this.mediator.subscribe(this.prefix + event, callback, context || this);
  };

  Proxy.prototype.off = function(event, callback, context) {
    this.mediator.unsubscribe(this.prefix + event, callback, context || this);
  };

  Proxy.prototype.trigger = function() {
    arguments[0] = this.prefix + arguments[0];
    this.mediator.trigger.apply(this.mediator, arguments);
  };

  return Proxy;

});

require(['proxy'], function(Proxy) {

  var proxy = new Proxy('sample:');

  // this access is secured and bound to the prefix
  // I cannot mess up with other events which do not belong to me
  proxy.on('log', function(message) { console.log(message); });
  proxy.trigger('log', 'hi hello');

  // unfortunately there still would be a hack to leave my event scope
  proxy.mediator.trigger('outerscope:test', 'blabla');

});

As you see it would be possible to access the internal used mediator object of the proxy prototype and to mess up with it…

I would now like to hide the mediator instance somehow but have no idea where.
I could store it in some normal variable inside the requirejs module callback but this would not work good with requirejs and could cause overlapping.

So what else can I do?

UPDATE:

define(['mediator'], function(Mediator) {

  var Proxy;

  var mediator = new Mediator();

  Proxy = function(prefix) {
    this.prefix = prefix;
  };

  Proxy.prototype.on = function(event, callback, context) {
    mediator.subscribe(this.prefix + event, callback, context || this);
  };

  Proxy.prototype.off = function(event, callback, context) {
    mediator.unsubscribe(this.prefix + event, callback, context || this);
  };

  Proxy.prototype.trigger = function() {
    arguments[0] = this.prefix + arguments[0];
    mediator.trigger.apply(this.mediator, arguments);
  };

  return Proxy;

});

require(['proxy'], function(Proxy) {

  var proxy = new Proxy('sample:');
  proxy.on('log', function(message) { console.log(message); });

});
  • 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-13T09:21:43+00:00Added an answer on June 13, 2026 at 9:21 am

    This is a typical example of encapsulation of variable within closures in Javascript. What you need is to define you mediator instance as a local variable in the same scope with the Proxy. This will allow the Proxy objects to have access to Mediator via closure but will isolate Mediator from the code outside your define-callback. So like this:

    define(['mediator'], function(Mediator) {
    
        // Make mediator local scope variable
        var mediator = new Mediator(),
    
        Proxy = function(prefix) {
            this.prefix = prefix;
        };
    
        Proxy.prototype.on = function(event, callback, context) {
            mediator.subscribe(this.prefix + event, callback, context || this);
        };
    
        // ... rest of the code
    
        return Proxy;
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got an application which needs to execute some booting/startup like: ajax dynamic requirejs
I've got the following which points to a backbone.d.ts definition. import Backbone = module(../../../dep/backbone/backbone);
I've got some nested objects like this: object Routes { object admin { def
I've got the following requireJS config. When trying to reference the package/ImagingX module I
I've got an ASP.NET app that requires Windows Integrated Security. For some reason, I
I've got a large XML file, which takes over 40 seconds to parse with
What is a good strategy for stopping a requireJS module from loading from information
I've got a Windows application running some expensive equipment; this application dies in a
Okay so I've got the hang of the basics writing classes and methods, and
I'm doing some LINQ which requires a custom comparer, so I created a new

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.