Subj : Re: JS_GetClass won't return proper class name. To : Mike Moening From : Brendan Eich Date : Mon Mar 28 2005 09:00 pm 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. > ---------------------------- > > .