JavaScript string prototype properties

WBOY
Release: 2023-09-01 10:49:02
forward
893 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.

  

Using the toUpperCase() and toLowerCase() methods of string prototype property to customize the strings

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.

  

Using the length property of the string prototype property to get the length of the string

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.

  

Adding the countWords() method to the string prototype property

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.

  

Customizing the toUpperCase() method of the string prototype property

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
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!