string.prototype
时间: 2023-10-01 14:11:51 浏览: 56
There is a typo in the question. The correct method name is `String.prototype`.
`String.prototype` is a property that represents the prototype for the String constructor. It allows you to add new methods and properties to all string objects.
For example, you can add a new method called `countOccurrences` to the `String.prototype` like this:
```javascript
String.prototype.countOccurrences = function(substring) {
let count = 0;
let index = this.indexOf(substring);
while (index !== -1) {
count++;
index = this.indexOf(substring, index + 1);
}
return count;
}
let str = "hello world";
console.log(str.countOccurrences("l")); // output: 3
```
This adds a new method `countOccurrences` to all string objects, which counts the number of occurrences of a substring within the string.