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

标签 javascript oop

好的 - 我正在尝试创建一个字符串库,其中包含 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/

相关文章:

c++ - 覆盖运算符 = 以便我们可以使用 = 复制类实例而不会出现浅拷贝问题

c# - 当您还需要 "default"具体依赖项时,哪种依赖项注入(inject)更好的模式?

javascript - Javascript 中的原型(prototype)有什么作用?

javascript - 在页面上显示某项的一个值,但发布不同的值

javascript - top.window.location Internet Explorer 权限被拒绝错误

javascript - 如何使用 Protractor 获取可见行(非 Angular )下的行数?

javascript - 我如何重构我的方法?

javascript - 我的 javascript 代码返回最常见的字母有什么问题?

javascript - 如何获取 jquery 中具有通用名称的复选框的值?

ruby - 启用标记为 'attr_reader' 的类实例属性的更改值