| 1 |
if (!Object.prototype.watchChange) { |
| 2 |
var isFunction = function (fn) { |
| 3 |
return fn && {}.toString.call(fn) === '[object Function]'; |
| 4 |
}; |
| 5 |
Object.defineProperty( |
| 6 |
Object.prototype, |
| 7 |
'watchChange', |
| 8 |
{ |
| 9 |
enumerable: false, |
| 10 |
configurable: true, |
| 11 |
writable: false, |
| 12 |
value: function (prop, handler) { |
| 13 |
var obj = this; |
| 14 |
|
| 15 |
function x(prop, handler) { |
| 16 |
var oldval = obj[prop], |
| 17 |
newval = oldval, |
| 18 |
getter = function () { |
| 19 |
return newval; |
| 20 |
}, |
| 21 |
setter = function (val) { |
| 22 |
return newval = handler.call(obj, prop, oldval, val); |
| 23 |
}; |
| 24 |
|
| 25 |
if (delete obj[prop]) { |
| 26 |
Object.defineProperty( |
| 27 |
obj, |
| 28 |
prop, |
| 29 |
{ |
| 30 |
get: getter, |
| 31 |
set: setter, |
| 32 |
enumerable: true, |
| 33 |
configurable: true |
| 34 |
} |
| 35 |
); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
if (isFunction(prop)) { |
| 40 |
for (var k in this) { |
| 41 |
new x(k, prop); |
| 42 |
} |
| 43 |
} else { |
| 44 |
new x(prop, handler) |
| 45 |
} |
| 46 |
} |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
if (!Object.prototype.unwatchChange) { |
| 51 |
Object.defineProperty( |
| 52 |
Object.prototype, |
| 53 |
'unwatchChange', |
| 54 |
{ |
| 55 |
enumerable: false, |
| 56 |
configurable: true, |
| 57 |
writable: false, |
| 58 |
value: function (prop) { |
| 59 |
var val = this[prop]; |
| 60 |
delete this[prop]; |
| 61 |
this[prop] = val; |
| 62 |
} |
| 63 |
} |
| 64 |
); |
| 65 |
} |