问题
Im trying to Add a few methods to the Javascript String Object. one of the main methods im trying to add is a substring count function
String.prototype.substring_count = function(delimiter){
return {THIS IS WHAT I NEED THE STRING FOR}.split(delimiter).length;
}
Where do you access the string in the String object?
回答1:
Use this
. In the documentation, it is mentioned
If the method is on an object's prototype chain, this refers to the object the method was called on
in this case, the string itself
String.prototype.substring_count = function(delimiter){
return this.split(delimiter).length;
}
console.log('test,123'.substring_count(','));
来源:https://stackoverflow.com/questions/33477092/javascript-prototype-string-accessing-string-value