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.
Using the toUpperCase() and toLowerCase() methods of string prototype property to customize the strings
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.
Using the length property of the string prototype property to get the length of the string
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.
Adding the countWords() method to the string prototype property
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.
Customizing the toUpperCase() method of the string prototype property
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!