Use Functions as an Object
by Mohan[ Edit ] 2012-09-20 21:54:39
Use Functions as an Object
function Counter() {
if ( !arguments.callee.count ) {
arguments.callee.count = 0;
}
return arguments.callee.count++;
}
Print( Counter() ); // prints: 0
Print( Counter() ); // prints: 1
Print( Counter() ); // prints: 2
Listen a property for changes
function onFooChange( id, oldval, newval ) {
Print( id + " property changed from " + oldval + " to " + newval );
return newval;
}
var o = { foo:5 };
o.watch( 'foo', onFooChange );
o.foo = 6;
delete o.foo;
o.foo = 7;
o.unwatch('foo');
o.foo = 8;
// prints:
// foo property changed from 5 to 6
// foo property changed from undefined to 7