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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:29:19+00:00 2026-06-05T02:29:19+00:00

I am trying to have my Node.js object cast events . Now this is

  • 0

I am trying to have my Node.js object cast events. Now this is not a problem if I make ‘static’ objects and instantiate them, but how do I do this when my object has no static grandpa, like when an object was created using object literal notation?

I am used to writing ExtJS syntax so I prefer everything in object literal.

// var EventEmitter = require('events').EventEmitter; // How where when?

myObject = {
  myFunction: function() {
    console.log('foo');
  },
  urFunction: function() {
    console.log('bar');
  }
};

This is an object. It does not have a constructor, because there don’t need to be more instances.

Now how do I allow myObject to emit events?

I have tried and tried to adapt code, but I cannot get it to work without rewriting my object to a form with a constructor like so:

var EventEmitter = require('events').EventEmitter;
var util = require('util');

function myClass() {
  EventEmitter.call(this);

  myFunction: function() {
    this.emit('foo');
  },
  urFunction: function() {
    this.emit('bar');
  }
};

myClass.prototype = Object.create(EventEmitter.prototype);
// or // util.inherits(myClass, EventEmitter);
var myObject = new myClass; // Shouldn't be necessary for my single-instance case

Or adding my functions/prototypes to something constructed like so:

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var myObject = new EventEmitter();
// or // var myObject = Object.create(new EventEmitter); // Dunno difference
myObject.myFunction = function() {
    this.emit('foo');
  },
myObject.urFunction = function() {
    this.emit('bar');
  }
};

util.inherits(myObject, EventEmitter);

How do I allow myObject to emit events, while keeping the object literal notation?
So many confusing ways to do it, but not one within those JSON-like notated objects.

  • 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-05T02:29:20+00:00Added an answer on June 5, 2026 at 2:29 am

    Why not use composition instead of inheritance?

    var myObject = {
      myFunction: function() {
        console.log('foo');
      },
      urFunction: function() {
        console.log('bar');
      },
      emitter: new EventEmitter()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn Node and have the function: this.logMeIn = function(username,stream) { if
I have a requirement to load a complex object called Node ...well its not
I'm trying to serialize an object that have a duplicate node name using C#,
I have another simple question about Node. I'm trying to make a simple http
I have been trying to read a node from YouTube respons. If ever delt
Okay, guys i have been trying to define a Stack , each node also
I have below updateFile code, here I am trying to add new node when
I'm trying to implement an http long polling server in Node.js, and have no
I have been trying to implement a CheckBox Node Tree, where the parent nodes
I'm trying to figure out how to have Spring Security return a UserDetails object

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.