Skip to content

Instantly share code, notes, and snippets.

@jgermade
Last active August 29, 2015 14:08
Show Gist options
  • Save jgermade/631a80dfc271a85bc0ba to your computer and use it in GitHub Desktop.
Save jgermade/631a80dfc271a85bc0ba to your computer and use it in GitHub Desktop.
Emitter static and dynamic
function Emitter (name) {
var emitter = this;
this.$name = name;
this.staticTriggeredBy = [];
this.staticTrigger = function () {
emitter.staticTriggeredBy.push({ $name: this.$name, name: this.name });
console.log('[static] $name: ' + this.$name + ', name: ' + this.name);
console.log(JSON.stringify(emitter.staticTriggeredBy));
};
this.dynamicTriggeredBy = [];
}
Emitter.prototype.dynamicTrigger = function () {
// this.dynamicTriggeredBy = this.dynamicTriggeredBy || [];
this.dynamicTriggeredBy.push({ $name: this.$name, name: this.name });
console.log('[dynamic] $name: ' + this.$name + ', name: ' + this.name);
console.log(JSON.stringify(this.dynamicTriggeredBy));
};
// inherited
function pEmitter (name) {
this.name = name;
}
pEmitter.prototype = new Emitter('foobar');
// tests
var e1 = new Emitter('foo'),
e2 = new Emitter('bar'),
e3 = new pEmitter('pFoo'),
e4 = new pEmitter('pBar');
e1.staticTrigger();
e1.dynamicTrigger();
e2.staticTrigger();
e2.dynamicTrigger();
e3.staticTrigger();
e3.dynamicTrigger();
e4.staticTrigger();
e4.dynamicTrigger();
@jgermade
Copy link
Author

console output

"[static] $name: foo, name: undefined"
"[{"$name":"foo"}]"
"[dynamic] $name: foo, name: undefined"
"[{"$name":"foo"}]"
"[static] $name: bar, name: undefined"
"[{"$name":"bar"}]"
"[dynamic] $name: bar, name: undefined"
"[{"$name":"bar"}]"
"[static] $name: foobar, name: pFoo"
"[{"$name":"foobar","name":"pFoo"}]"
"[dynamic] $name: foobar, name: pFoo"
"[{"$name":"foobar","name":"pFoo"}]"
"[static] $name: foobar, name: pBar"
"[{"$name":"foobar","name":"pFoo"},{"$name":"foobar","name":"pBar"}]"
"[dynamic] $name: foobar, name: pBar"
"[{"$name":"foobar","name":"pFoo"},{"$name":"foobar","name":"pBar"}]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment