Subj : [spidermonkey] getter/setter for all properties of an object To : netscape.public.mozilla.jseng From : franck Date : Fri Aug 05 2005 11:56 am Hello, I am tring to make a little object that allows me to listen all properties get and set of an object ( a kind of super watch ). All is working properly, however I noticed that even if I defined my own setProperty, the property is stored in the object ( I can see it using a for-in loop ). I wondering if it is possible to avoid the foo property ( in my example ) to be stored in the object. Regards, Franck. c++ code: -------- #include "jsapi.h" JSBool ClassObjectEx_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { jsval getter; JS_GetReservedSlot( cx, obj, 0, &getter ); jsval args[1] = { id }; JS_CallFunctionValue( cx, obj, getter, 1, args, vp ); return JS_TRUE; } JSBool ClassObjectEx_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { jsval setter; JS_GetReservedSlot( cx, obj, 1, &setter ); jsval args[2] = { id, *vp }; JS_CallFunctionValue( cx, obj, setter, 2, args, vp ); return JS_TRUE; } JSBool ClassObjectEx_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JS_SetReservedSlot( cx, obj, 0, argv[0] ); // getter function JS_SetReservedSlot( cx, obj, 1, argv[1] ); // setter function return JS_TRUE; } JSClass ClassObjectEx = { "ObjectEx", JSCLASS_HAS_RESERVED_SLOTS(2), JS_PropertyStub, JS_PropertyStub, ClassObjectEx_getProperty, ClassObjectEx_setProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, JSCLASS_NO_OPTIONAL_MEMBERS }; void InitClassObjectEx( JSContext *cx, JSObject *obj ) { JS_InitClass( cx, obj, NULL, &ClassObjectEx, ClassObjectEx_constructor, 0, NULL, NULL, NULL, NULL); } javascript code: --------------- function ObjGetter( prop ) { //Print( 'TRACE: getter: ' + prop ); return ReadValueFromDb( prop ); } function ObjSetter( prop, val ) { //Print( 'TRACE: setter: ' + prop + ' ' + val ); WriteValueToDb( prop, val ); } var obj = new ObjectEx( ObjGetter, ObjSetter ); obj.foo = 1234; for (var i in obj ) { Print( i + ':' + obj[i] ); } .