Subj : .prototype behaves two different ways (SpiderMonkey) To : netscape.public.mozilla.jseng From : Jason Straathof Date : Tue Jan 14 2003 04:12 pm Hello. Here's what I'm trying to do, with the SpiderMonkey engine: I want to be notified when a script sets properties on a function's prototype, which is useful for many reasons. In this case, I wish to update my user interface when certain "inherited" values change. To do this, I am using the setProperty pointer of a JSClass instance, and it mostly works very well. Consider (JavaScript): //--- // Works on instances of my super class... var myInstance = new SuperClass(); myInstance.color = "red"; // setProperty JSPropertyOp called nicely. // Define subclass... function SubClass() { // ... blah, blah ... } SubClass.prototype = new SuperClass(); // Set prototype property... SubClass.prototype.color = "red"; // No call to setProperty JSPropertyOp. Why? //--- The setProperty function is not called when I set any property of the prototype object - *in script*. If I perform a similar operation using the API, however, the thing works. Consider (C code): //--- // Variables... JSContext* cx; JSVal result; JSObject* classObj; JSObject* protoObj; // ... JS_GetProperty(cx, JS_GetGlobalObject(cx), "SubClass", &result); classObj = JSVAL_TO_OBJECT(result); protoObj = JS_GetPrototype(cx, classObj); JS_SetProperty(cx, protoObj, "color", JSVAL_VOID); // setProperty JSPropertyOp called as I would expect. // ... //--- Can anyone tell me why this would be the case? Why does the prototype work in these two very distinct ways? Most importantly, how can I get the prototype to work the way I expect, as it does with the API, from within the script (which is where it really counts)? If you are curious, I have also tried, within the script, assigning some variable the prototype, and performing the assignment there... var x = SubClass.prototype; x.color = "red"; // setProperty function not called. Additionally, sending the prototype to a function where the assignment is performed does not work... function setStinkinColor(o, color) { o.color = color; // JSPropertyOp not called when o is a prototype. } setStinkinColor(SubClass.prototype, "red"); // doesn't call setProperty JSPropertyOp. I am very confused. Any help will, of course, be greatly appreciated. Jason. .