如何在仍然返回值的同时打破自定义方法链?

How can I break a custom method chain while still returning a value?

我对我的搜索变量进行了替换,将已知组转换为组名。

我觉得,在每一步,我都可以进行正则表达式匹配,看看是否还有两个或更多组成员仍在搜索查询中,如果没有,则中止链。这是我经常使用的一个函数,它负责在没有剩余匹配项时跳出进程。

我的实际替换链有 15 个长,如果我能在第一或第二个跳出那个,似乎是合适的。

所以,我想我会写这样的东西

String.prototype.abortreplace = function (m,r) {
    var toreturn;
    if (this.match(/\b\w\b/g).length > 0) {
        toreturn = this.replace(m,r); 
    } else {
        return;
    }
    return toreturn;
}

tx = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p";
tx2 = tx.abortreplace(/a,b,c/g,"first three letters").abortreplace(/d,e,f/g,"second three letters").abortreplace(/g,h,i/g,"third three letters").abortreplace(/j,k,l/g,"fourth three letters").abortreplace(/m,n,o/g,"fifth three letters").abortreplace(/p,q,r/g,"sixth three letters");
alert(tx2);

这适用于这个特定的字符串,因为字符串末尾有 p,而且我将它设置为 length > 0。实际上,长度将为 length > 2。在那种情况下,它 returns undefined 并中断。 我很好奇我怎样才能 return 字符串并仍然断开链条。(我也试过 return false 其中 return 是错误的而不是未定义)。

String.prototype.abortreplace = function (m,r) {
    var toreturn;
    if (this.match(/\b\w\b/g).length > 2) {
        toreturn = this.replace(m,r); 
    } else {
        return;
    }
    return toreturn;
}

tx = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p";
tx2 = tx.abortreplace(/a,b,c/g,"first three letters").abortreplace(/d,e,f/g,"second three letters").abortreplace(/g,h,i/g,"third three letters").abortreplace(/j,k,l/g,"fourth three letters").abortreplace(/m,n,o/g,"fifth three letters").abortreplace(/p,q,r/g,"sixth three letters");
alert(tx2);

一个明确的解决方法是在条件不匹配时简单地return this,但这当然不会中断链,它只是否定每个后续步骤。

我也知道我可以做大致如下的事情:

var groups = ["a,b,c","d,e,f"]
var gnames = ["first three letters","second three letters"]
function chainreplace(query,step) {
  if (this.match(/\b\w\b/g).length > 0) {
    query = query.replace(groups[step],gnames[step]);
    if (step < groups.length) {
      query = chainreplace(query,step+1);
    }
    return query;
  }
}
chainreplace("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p",1);

但如果可能的话,我更喜欢链接方法,更容易重用(相似但不相同),而无需创建更多对象数组

与其将自己的方法放到 String 原型上(呃),不如想出自己的数据结构来做你想做的事。

下面的想法是,有一种类型的对象可以执行您想要的处理,returns 可链接对象,到时候,它可以 return 具有相同类型的不同类型使后续链接调用短路的接口:

var replacer = (function() {
  function fixedReplacer(str) {
    var r = {
      abortreplace: function() {
        // this abortreplace just returns the object it was called on
        return r;
      },
      toString: function() {
        return str;
      }
    };
    return r;
  }

  function replacer(str) {
    return {
      abortreplace: function(m, r) {
        return (str.match(/\b\w\b/g)||[]).length > 2 
          ? replacer(str.replace(m, r)) 
          : fixedReplacer(str);
      },
      toString: function() {
        return str;
      }
    };
  }
  
  return replacer;
})();

tx = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p";
tx2 = replacer(tx)
  .abortreplace(/a,b,c/g, "first three letters")
  .abortreplace(/d,e,f/g, "second three letters")
  .abortreplace(/g,h,i/g, "third three letters")
  .abortreplace(/j,k,l/g, "fourth three letters")
  .abortreplace(/m,n,o/g, "fifth three letters")
  .abortreplace(/p,q,r/g, "sixth three letters")
  .toString();
console.log(tx2);

当然,这并不能阻止所有 15 个方法调用的发生(正如 Felix 和 Bergi 指出的那样,不抛出异常是不可能的),但它可以显着减少执行的计算量。