Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_events is shared across subclasses #10

Closed
lpstein opened this issue May 30, 2014 · 8 comments
Closed

_events is shared across subclasses #10

lpstein opened this issue May 30, 2014 · 8 comments

Comments

@lpstein
Copy link

lpstein commented May 30, 2014

Behavior broke in node 0.10, see nodejs/node-v0.x-archive#7157 (fixed in node.js master).

Example:

var A = function() {};
A.prototype = new events.EventEmitter();

var a1 = new A();
var a2 = new A();

a1.on('test', function() {
  console.log('test');
});
a2.emit('test');

This logs 'test' where it shouldn't.

@defunctzombie
Copy link
Collaborator

Can we get a fix and test PR for this?
On May 30, 2014 6:57 PM, "lpstein" notifications@github.com wrote:

Behavior broke in node 0.10, see nodejs/node-v0.x-archive#7157
nodejs/node-v0.x-archive#7157 fixed in node.js master
vkurchatkin/node@2956f03
.

Example:

var A = function() {};A.prototype = new events.EventEmitter();
var a1 = new A();var a2 = new A();
a1.on('test', function() {
console.log('test');});a2.emit('test');

This logs 'test' where it shouldn't.


Reply to this email directly or view it on GitHub
#10.

@defunctzombie
Copy link
Collaborator

The fix from node.js master doesn't work in ie8. So if we want to keep parity with current browsers we support the fix has to be different. Personally don't care about dropping IE8 support.

josephfrazier pushed a commit to josephfrazier/SIP.js that referenced this issue Apr 27, 2015
@darkyen
Copy link

darkyen commented Jan 10, 2016

Drop IE 8 ? Please ?

@leewaygroups
Copy link

@lpstein Looking at your code example, a2.emit('test'); , logging 'test' is normal.

Reason
A.prototype = new events.EventEmitter(); implies that every instance of A will share among other things same copy of events collection -- Pure case of prototypal inheritance.

Suppose you do not want this, you can make instances have their own private events collection by:
var A = function() {
events.EventEmitter.call(this);
};

Note: this does not take away the shared events collection as explained earlier, it only shadows it.

Another option is to require util module then do this:
util.inherits(A, events.EventEmitter);

Cheers!!

@patrickroberts
Copy link

You can also do something like:

function A() {
  events.EventEmitter.call(this);
}

A.prototype = Object.create(events.EventEmitter.prototype);
A.prototype.constructor = A;

That's one of the methods Babel uses to transpile ES6 inheritance when Object.setPrototypeOf doesn't exist. And it doesn't just shadow it, it doesn't even create a shared events collection since Object.create() does not invoke the constructor!

@lpstein
Copy link
Author

lpstein commented Jul 23, 2016

This is an automatically generated message.

lpstein@yahoo-inc.com is no longer with Yahoo! Inc.

Your message will not be forwarded.

If you have a sales inquiry, please email yahoosales@yahoo-inc.com and someone will follow up with you shortly.

If you require assistance with a legal matter, please send a message to legal-notices@yahoo-inc.com

Thank you!

@darkyen
Copy link

darkyen commented Jul 24, 2016

Yahoooo spam!

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Jan 31, 2018

Fixed in #40. Will try to release soon once I get it working on IE8 again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants