 |
<!--You can define new functions for objects with prototype. This example shows you how.--> <HTML> <HEAD> <TITLE> Defining new functions for objects </TITLE> <script language="JavaScript"> <!-- // Define a new, not existing function for the string object // that sorts the characters of the seperate words in a string String.prototype.sort = function () { var mArray = this.split(" "); for (x = 0; x < mArray.length; x++) { mArray[x] = mArray[x].toLowerCase().split("").sort().join(""); } // Return the array as a string; return mArray.join(" "); } // doSort calls upon the new sort() function function doSort() { var Str = document.f.o.value; // Sort the characters of the string; document.f.s.value = Str.sort(); } //--> </script> </HEAD> <BODY> <form name="f"> Enter something here:<br> <input type="text" size="40" name="o"><br> Here's the sorted result:<br> <input type="text" size="40" name="s"> <hr noshade> <input type="button" value="Sort now!" onClick="doSort()"> </form> </BODY> </HTML>
|
|