Csaba Gabor wrote:
Richard Cornford wrote: Csaba Gabor wrote:
<snip> I tried to modify the String object itself, similarly to the
below (by giving it a self value, to override any original
value), but I didn't get it working (yet?).
<example>
That seems a very extension dependent method of doing what ECMAScript
should be able to do by using a String object as the prototype for
another object:-
<example>
- Or using a closure to make the string value of the object private:-
...
Thanks for the nice examples. But is it not possible to modify the
String prototype directly? The below code that I tried works in FF,
but it fails in IE 6: changeStr works as expected, changing .self on
In the beating a dead horse category, I took another approach. Rather
than trying to modify the String prototype directly (on account of IE
wanting to do a "copy" when it saw the string object), I thought
perhaps I could redefine it. And anyway the structure should be
cleaner. Once again, it works in FF and fails (though not so badly) in
IE.
But the failure in IE is still quite bad. Everything is OK except the
..length property is uniformly 0. That is to say, IE's jscript
steadfastly refuses to let me affix a .length property to the revised
String object. It wants to reflect the underlying prototype's .length,
as far as I can tell.
oldString = String;
String = function(st) { this.set(st); }
String.prototyp e = oldString.proto type;
String.prototyp e.toString = function() { return this.value; }
String.prototyp e.valueOf = function() { return this.value; }
String.prototyp e.set = function(replac ement, searchRE) {
if (arguments.leng th<2) this.value = oldString(repla cement);
else this.value = this.replace(se archRE, replacement); }
function changeStr (oStr, newStr) { oStr.set(newStr , /.*/); }
var u = new String('yyy');
var t = new String(u);
alert ("old t: " + t + "\nlength: " + t.length);
changeStr(t, 'xxu xxz');
alert(
'string [xxu xxz] = "' + t+'"'+
'\nlength [7] = '+t.length+
'\nindex of u [2] = '+t.indexOf('u' )+
'\nlast index of x [5] = '+t.lastIndexOf ('x')+
'\nsubstring(4, -1) [xxu ] = "'+t.substring( 4, -1)+'"'+
'\ncharAt(2) [u] = "'+t.charAt(2)+ '"'+
'\ncharCodeAt(3 ) [32] = '+t.charCodeAt( 3)+
'\nconcat(" ff", 4) [xxu xxz ff4] = '+t.concat(" ff", 4)+
'\nmatch("x") [x] = "'+t.match("x") +'"'+
'\nsearch("x") [0] = '+t.search("x") +
'\nslice(2, 5) [u x] = "'+t.slice( 2, 5)+'"'+
'\nsplit("x") [,,u ,,z] = '+t.split("x")+
'\ntoUpperCase( ) [XXU XXZ] = "'+t.toUpperCas e()+'"'
);
alert ("old u: " + u + "\nlength: " + u.length);