gpt4 book ai didi

Javascript 原型(prototype)字符串 - 访问字符串值

转载 作者:行者123 更新时间:2023-12-02 15:29:09 25 4
gpt4 key购买 nike

我正在尝试向 Javascript 字符串对象添加一些方法。我尝试添加的主要方法之一是子字符串计数函数

String.prototype.substring_count = function(delimiter){
return {THIS IS WHAT I NEED THE STRING FOR}.split(delimiter).length;
}

从哪里访问 String 对象中的字符串?

最佳答案

使用这个。在 documentation ,提到了

If the method is on an object's prototype chain, this refers to the object the method was called on

在本例中,是字符串本身

String.prototype.substring_count = function(delimiter){
return this.split(delimiter).length;
}

console.log('test,123'.substring_count(','));

关于Javascript 原型(prototype)字符串 - 访问字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com