
prototype属性可以让你的属性和方法添加到String对象。
注意:prototype是一个全局属性,几乎所有对象(Number, Array, String 和 Date 等)都可用。
语法:
String.prototype.name = value
创建一个新的字符串方法,该方法返回给定文本中的元音数量:
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 | 是 | 是 | 是 | 是 | 是 |
JavaScript String endsWith() 方法 JavaScript String constructor 属性




