Subj : Re: about defining object... To : Youngsun Jeong From : Brendan Eich Date : Tue Sep 30 2003 11:30 pm Youngsun Jeong wrote: >JSObject* InitMyobjectClass(JSContext *cx, JSObject *obj) >{ > JSObject *proto; > > proto = JS_InitClass(cx, obj, NULL, &my_class, NULL, 1, NULL, >my_method, NULL,NULL); > > There's your problem: you don't pass a non-null constructor parameter (after &my_class). > if(!proto) > return NULL; > return proto; >} > > Nit-picking, there's no reason for proto or the if and early null return. Just return JS_InitClass(...). >"my_class" has method "my_plusAge()", doesn't have any properties. >And I add "InitMyobjectClass" to jsapi.c. See below: > >file name : jsapi.c > ... >#include "myobject.h" //added > ... >JS_PUBLIC_API(JSBool) >JS_InitStandardClasses(JSContext *cx, JSObject *obj) >{ > ... > return js_InitArrayClass(cx, obj) && > js_InitBooleanClass(cx, obj) && > js_InitMathClass(cx, obj) && > js_InitNumberClass(cx, obj) && > js_InitStringClass(cx, obj) && > InitMyobjectClass(cx, obj) && //added > > Please don't do that. There's absolutely no reason. In *your* code, that calls JS_InitStandardClasses, just call your class init function after JS_InitStandardClasses returns successfully. > ... >} >What is wrong? > > See above, and more below. >And I want to know how do make the relation between "age" and >"plusAge()". > > Where do you call JS_SetPrivate on new objects of your class, to give them an "age" private datum? Since you pass NULL for the class constructor, there's no chance for your code to associate an age with each object via JS_SetPrivate. >Thank you for any assistance. > > What documentation are you studying, btw? /be .