Skip to content

Instantly share code, notes, and snippets.

@tnhu
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save tnhu/6844616007112d8c5431 to your computer and use it in GitHub Desktop.

Select an option

Save tnhu/6844616007112d8c5431 to your computer and use it in GitHub Desktop.
[kanji, jsface] Intercept property changed
function makeSetter(name, fn) {
return function() {
console.log('setter: ', name , this);
return fn.apply(this, arguments);
}
}
Class.plugins.$interceptPropertyChanged = function invoke(clazz, parent, api)
{
for (var key in api) {
var prop = api[key];
if (clazz.prototype && prop && prop.set) {
prop.set = makeSetter(prop, prop.set);
Object.defineProperty(clazz.prototype, key, prop);
}
}
}
var Foo = Class({
name: {
get: function() {
return this._name || 'Tan';
},
set: function(value) {
this._name = value;
},
enumerable: true,
configurable: true
}
});
var f = new Foo();
f.name = 1234; // intercept function should be called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment