Subj : Re: define new function in Class String (Spidermonkey) To : netscape.public.mozilla.jseng From : Blaubaer Date : Wed Jan 05 2005 01:55 am I want to add the function sprintf to the String type. var a = new String(); a.sprintf("Integer: %d", 1); My function sprintf works, an returns the result, but I also want to change the value of "a". Thx Blaubaer "Brendan Eich" schrieb im Newsbeitrag news:41DB3758.8090001@meer.net... > Blaubaer wrote: >> I want's to define a new function in jsstr.c and I want to change the >> value of the String. >> >> OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE); works but: >> OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, val); resets the value after >> leaving the funtion? :-( > > You don't want to do that. > > First, put extensions in separate files, and add your methods to the > object you get back in v from: > > JSBool ok; > jsval v; > const char Sp[] = "String.prototype"; > > ok = JS_EvaluateScript(cx, global, Sp, sizeof Sp - 1, NULL, 0, &v); > > Second, by changing the private data of a String object wrapper, you are > not affecting the characters in the primitive type string value. Nor can > you, for several good reasons including ECMA-262 and sharing sanity. > > Why do you think you want to mutate a string, anyway? > > /be .