I want to add prototype function in string object. I working with it but it is not uppercase the string value. Fiddle
String.prototype.myUcase=function()
{
return this.toUpperCase();
}
I want to add prototype function in string object. I working with it but it is not uppercase the string value. Fiddle
String.prototype.myUcase=function()
{
return this.toUpperCase();
}
In javascript String is not mutable. So, when you do this.toUpperCase() it does not change the current string. Instead, it creates a new string with the uppercased letters in it.
If you want your example to work, you can do (as suggested by elclanrs in the comments)
fruits = fruits.myUcase();
this[i]=this[i].toUpperCase(); this particular line, modifies each and every element. That is why you are able to see the result.
myUcaseto a variable, you simply ignore it.toUpperCasedoesn't modify the string itself, it returns a new string.fruits = fruits.myUcase()