我如何替换原型(prototype)并使下面的相同代码工作?换句话说,我想在不使用原型(prototype)的情况下编写具有相同含义/逻辑的代码。
String.prototype.replaceAt = function(index, character) {
return this.substr(0, index) + character +
this.substr(index + character.length);
}
最佳答案
将其创建为自定义实用程序对象的方法 -
var StringUtils = {};
StringUtils.replaceAt = function (str, index, character) {
return str.substr(0, index) + character +
str.substr(index + character.length);
}
然后这样调用它——
var str = "Hello";
StringUtils.replaceAt(str, 0, "A");
关于javascript - Javascript 中的原型(prototype)替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158634/