Skip to content

Instantly share code, notes, and snippets.

@theadam
Created April 27, 2015 15:26
Show Gist options
  • Save theadam/9ca4f07665970bf925b6 to your computer and use it in GitHub Desktop.
Save theadam/9ca4f07665970bf925b6 to your computer and use it in GitHub Desktop.
class EventEmitter{
emit(event){
console.log(event);
}
}
function emits(obj, prop, descriptor){
let value; delete descriptor.writable;
delete descriptor.value;
descriptor.get = () => value;
descriptor.set = (v) => {
value = v;
obj.emit(prop + " CHANGE EVENT")
};
return descriptor;
}
var objTest = Object.create(EventEmitter.prototype);
EventEmitter.call(objTest);
var descriptor = emits(objTest, 'valueInObject', {});
Object.defineProperty(objTest, 'valueInObject', descriptor);
// Works. Logs 'valueInObject CHANGE EVENT'
objTest.valueInObject = 10;
class Test extends EventEmitter{
@emits
valueInClass;
}
// Does not work. Throws Error.
new Test().valueInClass = 100;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment