prototype屬性可以讓你的屬性和方法添加到String對象。
注意:prototype是一個全局屬性,幾乎所有對象(Number, Array, String 和 Date 等)都可用。
String.prototype.name = value
創(chuàng)建一個新的字符串方法,該方法返回給定文本中的元音數(shù)量:
String.prototype.countVowels = function() {
var x = this.match(/[aeiou]/gi);
return (x === null ? 0 : x.length);
};在字符串上使用新方法:
var str = 'Hello world'; str.countVowels(); // return 3
所有瀏覽器完全支持prototype屬性:
| 屬性 | ![]() | ![]() | ![]() | ![]() | ![]() |
| prototype | 是 | 是 | 是 | 是 | 是 |