Home > Web Front-end > JS Tutorial > body text

JavaScript string prototype properties

WBOY
Release: 2023-09-01 10:49:02
forward
891 people have browsed it

JavaScript 字符串原型属性

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.

grammar

Users can add any method to the string prototype property according to the following syntax.

String.prototype.method_name = function () {
   // function code
};
Copy after login

In the above syntax, method_name should be the name of the method we want to add to the string prototype.

Example 1 (using toUpperCase() and toLowerCase() methods)

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>
Copy after login

Example 2 (using the Length property)

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>
Copy after login

Example 3 (Adding methods to the string prototype)

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>
Copy after login

Example 4 (Default method of custom string prototype)

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!