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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:40:16+00:00 2026-05-27T01:40:16+00:00

I am trying to make a simple event handler. (Note, I’m not trying to

  • 0

I am trying to make a simple event handler.

(Note, I’m not trying to implement a full-blown publish/subscribe model; I’m just interested in why my example doesn’t work as I think it should)

var myObj = (function () {

  var private = "X";

  function triggerEvent(eventName) {
    if (this[eventName]) {
      this[eventName]();
    }
  }

  // Setter / Getter
  function getProp() {
    return private;
  }

  function setProp(value) {
    private = value;
    triggerEvent("onPropChange");
  }

  // Public API
  return {
    // Events
    "onPropChange": null,    // Fires when prop value is changed

    // Methods
    "getProp": getProp,
    "setProp": setProp
  };
})();

// Now set event handler
myObj.onPropChange = function () {
  alert("You changed the property!");
};

myObj.setProp("Z");  // --> Nothing happens. Wrong
                     // Why doesn't my alert show?

I set the onPropChange property of my object to a simpler handler function but it is not being fired. I have debugged this and it seems that in triggerEvent the variable this is referencing the global window object. I thought it should reference myObj (which is what I need).

Can someone explain the error in my thinking and how I correct this? Help much appreciated.

jsFiddle here

  • 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-27T01:40:16+00:00Added an answer on May 27, 2026 at 1:40 am

    Change your code so you create the obj and then return it like this:

    var myObj = (function () {
    
      var private = "X";
      var obj = {
        "onPropChange": null,    // Fires when prop value is changed
    
        // Methods
        "getProp": getProp,
        "setProp": setProp
      };
    
      function triggerEvent(eventName) {
        if (obj[eventName]) {
          obj[eventName]();
        }
      }
    
      // Setter / Getter
      function getProp() {
        return private;
      }
    
      function setProp(value) {
        private = value;
        triggerEvent("onPropChange");
      }
    
      return obj;          
    })();
    

    This way you have a reference to the object for the private functions.

    Fixed fiddle – http://jsfiddle.net/infernalbadger/dkqUW/2/

    Another way to fix it that you might prefer is to use the .call function:

    function setProp(value) {
        private = value;
        triggerEvent.call(this, "onPropChange");
    }
    

    This specifies what this will be in the method being called (triggerEvent).

    http://jsfiddle.net/infernalbadger/dkqUW/3/

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

Sidebar

Related Questions

I am trying to implement an event handler of sort. I am try to
I'm trying to make some keen experiments about android event handler... Let say.., I
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make some simple XML with Java and org.w3c.dom, but I got
I'm trying to make a simple drawing app in c++, but i'm having trouble
I'm trying to make a simple php script to find all src attributes from
I am trying to make a simple script to automate and log synchronization via
I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.
I am trying to make a simple redirector controller in CakePHP. I'd like the

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.