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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:38:15+00:00 2026-06-03T13:38:15+00:00

In the following implementation of the mediator pattern why this.name in the initialize method

  • 0

In the following implementation of the mediator pattern why this.name in the initialize method always undefined? As I was expecting it to be TestObject. How can I achieve this?

Also how can I create new instances of TestObject?

Mediator = function() {

        var debug = function() {
            // console.log or air.trace as desired
        };

        var components = {};

        var broadcast = function(event, args, source) {
            var e = event || false;
            var a = args || [];
            if (!e) {
                return;
            }
            //debug(["Mediator received", e, a].join(' '));
            for (var c in components) {
                if (typeof components[c]["on" + e] == "function") {
                    try {
                        //debug("Mediator calling " + e + " on " + c);
                        var s = source || components[c];
                        components[c]["on" + e].apply(s, a);
                    } catch (err) {
                        debug(["Mediator error.", e, a, s, err].join(' '));
                    }
                }
            }
        };

        var addComponent = function(name, component, replaceDuplicate) {
            if (name in components) {
                if (replaceDuplicate) {
                    removeComponent(name);
                } else {
                    throw new Error('Mediator name conflict: ' + name);
                }
            }
            components[name] = component;
        };

        var removeComponent = function(name) {
            if (name in components) {
                delete components[name];
            }
        };

        var getComponent = function(name) {
            return components[name] || false;
        };

        var contains = function(name) {
            return (name in components);
        };

        return {
            name      : "Mediator",
            broadcast : broadcast,
            add       : addComponent,
            rem       : removeComponent,
            get       : getComponent,
            has       : contains
        };
    }();


    // Components    
    Mediator.add('TestObject', function() {

        var someNumber = 0; // sample variable
        var someString = 'another sample variable';

        return {
            onInitialize: function() {
                // this.name is automatically assigned by the Mediator
                alert(this.name + " initialized.");
            },
            onFakeEvent: function() {
                someNumber++;
                alert("Handled " + someNumber + " times!");
            },
            onSetString: function(str) {
                someString = str;
                alert('Assigned ' + someString);
            }
        }
    }());

    Mediator.broadcast("Initialize");                 
    Mediator.broadcast('FakeEvent');                  
    Mediator.broadcast('SetString', ['test string']); 
    Mediator.broadcast('FakeEvent');                  
    Mediator.broadcast('SessionStart');
  • 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-03T13:38:16+00:00Added an answer on June 3, 2026 at 1:38 pm

    It’s because in the function bloc you’re returning, this represents the bloc itself, not the mediator object (you can try a console.log(this); in onInitialize to see that).

    EDIT:

    To add a name component to your callback, you could do something like that

    var addComponent = function(varName, component, replaceDuplicate) {
        if (varName in components) {
            if (replaceDuplicate) {
                removeComponent(varName);
            } else {
                throw new Error('Mediator name conflict: ' + varName);
            }
        }
        components[varName] = component;
        components[varName].name = varName;
    };
    

    There were a lot of name, so i changed the local name to varName

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

Sidebar

Related Questions

Given I have two File objects I can think of the following implementation: public
How can the following simple implementation of sum be faster? private long sum( int
I have the following implementation: As you can see I have a repeater (listing
Actually I come up with the following implementation bool DoesNamedDataSlotsExist(string name) { try {
this question is just speculative. I have the following implementation in C++: using namespace
I recently saw the following implementation of enqueue for a BlockingQueue ( source )
I have a problem with the following implementation of hook_cron in Drupal 6.1.3. The
I have the following code implementation of my generic singleton provider: public sealed class
There is a class A and it has the following operator() implementation: void A::operator()(...parameters...)
I would like to change the contract of the following Quicksort implementation to return

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.