Created
April 27, 2015 15:26
-
-
Save theadam/9ca4f07665970bf925b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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