Ok peep's so I know it's bad practice to mess with prototypes but here it is anyway...
Array.prototype.rev=
function(){
this.reverse();
}
Works fine! Updates the source array variable, ary
, as expected eg:
ary = [123, 456];
ary.rev();
// result: ary == [456, 123]
My problem comes when writing a similar property for String
.
What I would like to do is something like this...
String.prototype.rev=
function(){
this.split('');
this.reverse();
this.join('');
}
Seems simple enough right! Split the string, reverse it, and join it back together so the original string variable, str
, is a reverse of it's former self, as was with the ary
above!
Thing is: Although this.split()
has been called it needs to be stored as a variable, ie:
split = this.split('');
And there-in lies the this = this
problem...
Now split
is defined, it takes the focus away from editing the source variable and it's not like I can just say at the end of the function:
this = split;
As this
is 'immutable' or what ever it is when they mean it is static and unchangeable?
Getting to the point! My Question is this...
str = 'abc'
I want to be able to say str.rev()
not str = str.rev()
and get the result of str = 'cba'
where str === 'cba'
, catch my drift?!
All work-around's and tuition welcome peep's, I just ask that u know what ur talkin' 'bout. thx