JavaScript 字串原型屬性
在 JavaScript 中,每個物件都有其自身的屬性,並且每個物件都包含原型屬性。字串在 JavaScript 中也是一個物件。因此,它也包含原型屬性。
原型屬性巢狀在物件中,這意味著每個原型屬性都包含另一個原型屬性。字串物件的原型屬性包含預設方法和屬性。但是,開發人員可以自定義原型屬性,並向字串原型新增方法和屬性。
在本教程中,我們將學習如何使用字串原型屬性的方法並對其進行自定義。
語法
使用者可以按照以下語法將任何方法新增到字串原型屬性。
String.prototype.method_name = function () { // function code };
在上述語法中,method_name 應該是我們想要新增到字串原型的方法的名稱。
示例 1(使用 toUpperCase() 和 toLowerCase() 方法)
在下面的示例中,我們使用了字串原型屬性的 toUpperCase() 和 toLowerCase() 方法,它們分別將字串轉換為大寫和小寫。
在輸出中,使用者可以看到結果字串。
<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>
示例 2(使用 Length 屬性)
在下面的示例中,我們演示瞭如何使用字串原型屬性的預設屬性。在這裡,我們使用了“length”屬性來計算字串中字元的總數。
在輸出中,我們可以看到字串的總字元數或長度,這是我們使用 length 屬性計算得出的。
<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>
示例 3(向字串原型新增方法)
我們還可以向字串原型屬性新增自定義方法。在這裡,我們向字串原型添加了 countWords() 屬性,它計算字串中單詞的總數並返回其值。
在這裡,我們使用“ ”作為分隔符分割字串,並計算結果陣列的長度以計算字串中單詞的總數。在程式碼中,我們可以看到,我們可以透過將任何字串作為引用來執行 countWords() 方法,就像其他字串方法一樣。
<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>
示例 4(自定義字串原型的預設方法)
在這個例子中,我們演示瞭如何自定義字串原型的預設方法。在這裡,我們自定義了 toUpperCase() 方法。通常,toUpperCase() 方法在將所有字串字元轉換為大寫後返回字串。
我們對其進行了自定義,以便在僅將第一個字元轉換為大寫後返回字串。如果第一個字元已經是大寫,則返回相同的字串。否則,我們使用 ASCII 值將第一個字元轉換為大寫。
在輸出中,我們可以看到 toUpperCase() 方法只將字串的第一個字元轉換為大寫,而不是將整個字串轉換為大寫。
<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>
使用者學習瞭如何使用字串原型屬性。我們可以使用它向字串物件新增方法和屬性。此外,我們還可以使用它來自定義字串屬性和方法。在將方法新增到字串原型之後,我們可以透過將字串作為引用來呼叫該方法。