Consider the code below :
function createFoo() {
var val;
function Foo() {}
Object.defineProperty(Foo.prototype, 'foo', {
get: function () {
return val;
},
set: function (value) {
val = value;
document.write('foo value set to : ', value);
}
});
return new Foo();
}
var foo = createFoo();
Object.observe(foo, function (changes) { document.write(changes); });
foo.foo = 'bar';
Why is it that Object.observe's handler is never fired? Can an object prototype be "observed"?
(Please, answers must not suggest to use some kind of third party library.)
Update
Please, read the comments for more information and for the resolution to this problem.
Object.observeproposal was withdrawn: esdiscuss.org/topic/an-update-on-object-observe .EventEmitterand emitting events instead. I'm choosing the single answer as there is no other answer anyway, so this question can be closed for posterity.