【问题标题】:How to replace jQuery $.trim()?如何替换 jQuery $.trim()?
【发布时间】:2019-01-15 01:02:00
【问题描述】:

我在一个网站中使用 jQuery,有一个内置 String.trim() 的 polyfill。网站过去经常在 IE 8 中运行并且需要 polyfill,但现在不再需要了。不幸的是,我无法从页面中删除 polyfill——我没有权限触摸它,我也无法删除它——所以这段代码在任何事情之前运行我可以控制:

String.prototype.trim = function() {
    return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "")
}

然后 jQuery 出现并执行此操作,但没有意识到本机 String.trim 已经搞砸了:

// Use native String.trim function wherever possible
trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
    function( text ) {
        return text == null ?
            "" :
            core_trim.call( text );
    } :

    // Otherwise use our own trimming functionality
    function( text ) {
        return text == null ?
            "" :
            ( text + "" ).replace( rtrim, "" );
    },

到目前为止,这并不是什么大问题,但我在 jQuery 中使用Datatables plugin,它有很多地方在不是字符串的数据上调用$.trim(),比如数字或数组。 IE 11 和 Chrome(我们的目标浏览器)中的本机代码只知道返回 $.trim(6) 的值,但 polyfill 不知道。

我尝试使用应该可以工作的函数重新定义原型:

String.prototype.trim = function(){
   if(typeof this.valueOf(this) === 'string'){
       return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
   } else {
       return this.valueOf(this);
   }
} 

但这不起作用,因为 jQuery 已经使用 polyfill 进行了扩展,并且对原型的进一步更改不会改变 jQuery 正在使用的内容。

我尝试按照this thread 重新定义$.trim(),但没有成功。

有没有办法将 String.prototype.trim() 返回到其本机代码?

有没有办法重新定义 $.trim()?

还有什么我没想到的想法?

【问题讨论】:

    标签: javascript jquery polyfills


    【解决方案1】:

    您可以覆盖 jQuery 核心方法

    (function(){
        var string = "       TRIM ME      ";
        console.log(jQuery.trim(string));
        // Define overriding method.
        jQuery.trim = function(string){
            return string;
        }
        console.log(jQuery.trim(string));
    })();
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    【讨论】:

      【解决方案2】:

      只需使用 String.prototype.trim() 覆盖 jQuery 的 $.trim(),然后使用您的函数覆盖 String.prototype.trim()

      var trimText = "       Some text       ";
      
      String.prototype.trim = function() {
        return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "")
      }
      
      $.trim = function(string) {
        return String.prototype.trim(string);
      }
      
      trimText = trimText.trim();
      
      console.log(trimText);
      <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

      【讨论】:

      • 这给了我一个最大调用堆栈超出错误,但它帮助我想出了答案。我会给你功劳。
      【解决方案3】:

      根据 Jack Bashford 的回答,我想出了这个。

      String.prototype.trim = function(){
        if(typeof this.valueOf(this) === 'string'){
          return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
        } else {
          return this.valueOf(this);
        }
      };
      
      $.trim = function(e){
          return String.prototype.trim.call(e);
      };
      

      最初的部分问题是我需要修复它,以便在调用 $.trim(number) 或 $.trim(array) 时它不会抛出错误。

      【讨论】:

        猜你喜欢
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-18
        • 2011-03-27
        • 2011-10-10
        • 2012-09-23
        • 2021-07-05
        • 1970-01-01
        相关资源
        最近更新 更多