Subj : Re: JS_GetClass won't return proper class name. To : netscape.public.mozilla.jseng From : Mike Moening Date : Mon Mar 28 2005 11:33 pm Brendan you rock! In short here is what I did: ctor = JS_GetConstructor(cx, JS_GetPrototype(cx, obj)); name = JS_GetFunctionId((JSFunction *) JS_GetPrivate(cx, ctor)); name comes back as "Factorial" Works like a charm! One more followup if you have the time: Unfortunately the "results" and "n" properities show up in the properties listing twice. Once standing alone and once under the Factorial "object" when I recursively call GetPropertyDescArray() on the object. How do I filter out the top level properties (I want them to show as properties of the Factorial object). The properties list looks stupid if they are shown twice. Thank you sir! "Brendan Eich" wrote in message news:4248E0EA.9020602@meer.net... > Mike Moening wrote: > > See code snipet below. > > > > When constructing a properties tree (name, type, value) for the current > > stack frame and calling JS_GetPropertyDescArray and iterating the jsval's, > > the Factorial "object" is seen as type JSTYPE_OBJECT (which is expected). > > > > After converting the jsval to a JSObject* and calling JS_GetClass() the name > > member always comes back as "Object". > > > Presumably you are talking about the new Factorial() result stored in > oFact, not the function object named Factorial. > > If you want the name of the class constructor and you have a prototype > or instance in hand, call it proto_or_instance, use > > JSObject *ctor; > JSString *name; > > ctor = JS_GetConstructor(cx, proto_or_instance); > name = JS_GetFunctionId((JSFunction *) JS_GetPrivate(cx, ctor)); > > > > How do I get JS_GetClass() to return "Factorial" so I can show the proper > > name? What other trick can I use to return the name of the "object"? > > > The class of instances of user-defined constructors is Object, per > ECMA-262 and JS as I implemented it. > > You want the constructor name, which is available too. > > > > Note: If I call GetPropertyDescArray() on the jsval representing > > "Factorial" it gives be back two properties named "n" and "results". After > > execution each has the proper name, type and value(5 and 120). > > > That's compatibility cruft for non-ECMA behavior that goes back to JS > 1.0 (in Netscape 2). > > /be > > > > > Thanks in advance! > > > > ------------------------------ > > function Factorial(n) > > { > > if(n!=null) > > this.n=n; > > else > > this.n=0; > > this.results=0; > > var self = this; > > function factor(i) { > > if (i <= 1) > > return 1; > > return i * factor(i-1) > > } > > this.execute = function() { > > this.results = factor(this.n); > > } > > } > > Factorial.prototype.setN = function(n) { > > this.n = n; > > } > > > > var oFact = new Factorial(); //Create our "object" > > oFact.setN(5); //Set the private data. > > oFact.execute(); //Call the method. > > ---------------------------- > > > > .