Fri, 17 May 2024


How to define a function that extends the capabilities of String objects?

By: Nathalie, NetArt Media
Wed, 1 September 2021

<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT>
// Define a function that extends the capabilities of String objects
function reverse()
{
myArray = new Array(this.length);

for(myEnum=0; myEnum<this.length; myEnum++)
{
myArray[myEnum] = this.substr(myEnum,1)

}

myArray.reverse();

return myArray.join("");

}

// Register the new function
String.prototype.reverse = reverse;
// Create a string object and test the String.reverse() method
myString = new String("ABCDEFGH");
document.write(myString.reverse())
document.write("<BR>")
</SCRIPT>
</BODY>
</HTML>


Category: Javascript
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox