经过一番修修补补,我已经或多或少地实现了我所希望的目标
const EventEmitter = require( 'events' );
class SyncedObject extends EventEmitter {
constructor( obj ) {
super();
this._object = obj;
return new Proxy( this, {
set: ( object, key, value, proxy ) => {
if( this._object[key] === value )
return false;
this._object[key] = value;
if( key[0] === '_') {
var eventArgs = {
propertyName: key,
oldValue: object[key],
newValue: value
};
this.emit( 'propertyChanged', eventArgs );
}
return true;
},
get: ( object, key ) => {
return this._object[key] || object[key];
}
});
}
silentUpdate( data ) {
for(var i = 0, keys = Object.keys(data); i < keys.length; i++)
this._object[keys[i]] = data[keys[i]];
}
}
不是最防弹的解决方案(EventEmitter属性引发事件),但对于我需要的,它可能会起作用。如果有人有改进建议,请发帖子。我暂时不回答这个问题。