Home > Article > Web Front-end > JavaScript string prototype properties
In JavaScript, each object has its own properties, and each object contains the prototype attribute. A string is also an object in JavaScript. Therefore, it also contains prototype properties.
Prototype properties are nested within objects, which means that each prototype property contains another prototype property. The prototype properties of a string object contain default methods and properties. However, developers can customize prototype properties and add methods and properties to the string prototype.
In this tutorial, we will learn how to use string prototype properties and customize them.
Users can add any method to the string prototype property according to the following syntax.
String.prototype.method_name = function () { // function code };
In the above syntax, method_name should be the name of the method we want to add to the string prototype.
In the following example, we use the toUpperCase() and toLowerCase() methods of the string prototype property to convert the string to uppercase and lowercase respectively.
In the output, the user can observe the resulting string.
<html> <body> <h3> Using the <i> toUpperCase() and toLowerCase() </i> methods of string prototype property to customize the strings </h3> <div id = "output"> </div> <script> let output = document.getElementById("output"); var str = "Hello readers! This string contains uppercase and lowerCase letters."; var res = str.toUpperCase(); var res1 = str.toLowerCase(); output.innerHTML = "Original string: " + str + "<br>" + "Uppercase string: " + res + "<br>" + "Lowercase string: " + res1; </script> </body> </html>
In the following example, we demonstrate how to use the default properties of the string prototype property. Here we use the "length" property to count the total number of characters in the string.
In the output, we can see the total number of characters or length of the string that we calculated using the length property.
<html> <body> <h3> Using the <i> length </i> property of the string prototype property to get the length of the string </h3> <div id = "output"> </div> <script> let output = document.getElementById("output"); let string = "JavaScript string contains prototype property."; let len = string.length;; output.innerHTML = "The original string is: " + string + "<br><br>"; output.innerHTML += "The length of the string is: " + len; </script> </body> </html>
We can also add custom methods to string prototype properties. Here, we have added the countWords() property in the string prototype, which counts the total number of words in the string and returns its value.
Here, we split the string using "" as delimiter and calculate the length of the resulting array to calculate the total number of words in the string. In the code, we can see that we can execute the countWords() method with any string as a reference just like other string methods.
<html> <body> <h3> Adding the <i> countWords() method to the </i> string prototype property </h3> <div id = "output"> </div> <script> let output = document.getElementById("output"); // adding countWords() method to string prototype property. String.prototype.countWords = function () { return this.split(" ").length; }; let string1 = "This is a string"; let string2 = "Hey, do you know how many words are there in this string?"; output.innerHTML = "The number of words in the string '" + string1 + "' is " + string1.countWords() + "<br>"; output.innerHTML += "The number of words in the string '" + string2 + "' is " + string2.countWords(); </script> </body> </html>
In this example, we demonstrate how to customize the default method of the string prototype. Here, we have customized the toUpperCase() method. Generally, the toUpperCase() method returns a string after converting all string characters to uppercase.
We have customized it to return the string after converting only the first character to uppercase. Here, we will return the same string if the first character is already uppercase. Otherwise, we convert the first character to uppercase using the ASCII value.
In the output, we can see that the toUpperCase() method only converts the first character of the string to uppercase instead of the entire string.
<html> <body> <h3> Customizing the <i> toUpperCase() method of the </i> string prototype property </h3> <div id = "output"> </div> <script> let output = document.getElementById("output"); // Customizing the toUpperCase() method of the string prototype property to capitalize only the first character String.prototype.toUpperCase = function () { if (this.charCodeAt(0) >= 97 && this.charCodeAt(0) <= 122) { // convert to uppercase let ascii = this.charCodeAt(0) - 32; return String.fromCharCode(ascii) + this.slice(1); } else { return this; } } let str = "first letter of this string should be capitalized"; output.innerHTML = "Original string: " + str + "<br>" + "Capitalized string: " + str.toUpperCase(); </script> </body> </html>
Users learned to use string prototype properties. We can use it to add methods and properties to string objects. Additionally, we can use it to customize string properties and methods. After adding the method to the string prototype, we can call the method using the string as a reference.
The above is the detailed content of JavaScript string prototype properties. For more information, please follow other related articles on the PHP Chinese website!