gpt4 book ai didi

JavaScript 字符串库——遇到一个小障碍

转载 作者:行者123 更新时间:2023-11-30 13:40:01 25 4
gpt4 key购买 nike

好的 - 我正在尝试创建一个字符串库,其中包含 JavaScript 中缺少的一些有用的东西。这是我目前所拥有的:

function $__STRING__$(in_string) { this.s = in_string; }
$__STRING__$.prototype = {
uppercase: function(){this.s = this.s.toUpperCase(); return this;},
lowercase: function(){this.s = this.s.toLowerCase(); return this;},
trim: function(){this.s = this.s.replace(/^\s+|\s+$/g,""); return this;},
ltrim: function(){this.s = this.s.replace(/^\s+/,""); return this;},
rtrim: function(){this.s = this.s.replace(/\s+$/,""); return this;},
striptags: function(){this.s = this.s.replace(/<\/?[^>]+(>|$)/g, ""); return this;},
escapetags: function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;},
unescapetags: function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;},
underscorize: function(){this.s = this.s.replace(/ /g,"_"); return this;},
dasherize: function(){this.s = this.s.replace(/ /g,"-"); return this;},
spacify: function(){this.s = this.s.replace(/_/g," "); return this;},
left: function(length){this.s = this.s.substring(length,0); return this;},
right: function(length){this.s = this.s.substring(this.s.length,this.s.length-length); return this;},
shorten: function(length){if(this.s.length<=length){return this.s;}else{this.left(this.s,length)+"..."; return this;}},
mid: function(start,length){return this.s.substring(start,(length+start));},
_down: function(){return this.s;},
contains: function(needle){if(this.s.indexOf(needle)!==-1){return true;}else{return false;}},
startswith: function(needle){if(this.left(this.s,needle.length)==needle){return true;}else{return false;}},
endswith: function(needle){if(this.right(this.s,needle.length)==needle){return true;}else{return false;};},
toString: function(){return this.s;}
}

function $E(in_string){return new $__STRING__$(in_string);}
String.prototype._enhance = function(){return new $__STRING__$(this);};
String.prototype._up = function(){return new $__STRING__$(this);};

它工作得很好,我可以链接命令等。

我对其进行了设置,以便可以通过以下两种方式将字符串转换为增强字符串:

$E('some string');
'some string'._enhance();

但是,每次我想使用内置的字符串方法时,我都需要先将其转换回字符串。所以现在,我输入了 _down()_up() 方法,如下所示:

alert( $E("hello man").uppercase()._down().replace("N", "Y")._up().dasherize() ); 
alert( "hello man"._enhance().uppercase()._down().replace("N", "Y")._up().dasherize() );

它工作正常,但我真正想做的是能够使用字符串可以使用的所有内置函数。我意识到我可以在我的对象中复制每个函数,但我希望有更简单的方法。

所以问题是,有没有一种简单的方法可以做到这一点?

谢谢-

最佳答案

你可以循环遍历 String.prototype 中的方法:

for(var name in String.prototype) {
if (typeof String.prototype[name] !== "function") continue;
if (name in $__STRING__$.prototype) continue;

addToProto(name);
}

function addToProto(name) {
$__STRING__$.prototype[name] = function() {
this.s = String.prototype[name].apply(this.s, arguments);
return this;
};
}

请注意,这不会正确处理像 indexOf 这样的方法;你可以这样修改它:

$__STRING__$.prototype[name] = function() {
var retVal = String.prototype[name].apply(this.s, arguments);
if (typeof retVal === "string") {
this.s = retVal;
return this;
} else
return retVal;
};

此外,您应该将显式方法移动到原型(prototype)中,如下所示:

 $__STRING__$.prototype.uppercase = function(){this.s = this.s.toUpperCase(); return this;};

编辑:for .. in 循环将无法正常工作(String.prototype 中的方法不可枚举)。

相反,您需要为每个方法名称手动调用 addToProto,如下所示:

addToProto('replace');

关于JavaScript 字符串库——遇到一个小障碍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2804112/

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