Last active
August 29, 2015 14:21
-
-
Save tnhu/6844616007112d8c5431 to your computer and use it in GitHub Desktop.
[kanji, jsface] Intercept property changed
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
| 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