我在打字稿文件中定义了以下车把帮手。

当给辅助函数提供字符串"test"10的maxResidualLength时,尽管检查员告诉我clippedString.length > maxResidualLength等于false,但仍会命中调试器语句

    Handlebars.registerHelper("ClipYearsFromString", function (strg: string, maxResidualLength:number) {
        var pattern = /(\b\d{4}.\b|\b\d{4}\b)/g; //remove sets of four digits or four digits with a non alpha character behind them.

        var clippedString = strg.replace(pattern, "");

        if (typeof (maxResidualLength) == "number" && maxResidualLength > 0)
        {
            if (clippedString.length > maxResidualLength);
            {
                debugger;
                clippedString = clippedString.substr(0, maxResidualLength) + "…";
            }
        }
        return clippedString;
    });


在模板中调用助手的方式如下:

   <span>{{{ClipYearsFromString SelectedRankingPool.PoolName 10}}}</span>


SelectedRankingPool.PoolName始终是一个字符串。

这是怎么回事?
在所有主流浏览器中,无论是Chrome,Firefox还是IE,都会发生相同的行为
,并且如果我在typescript和handlebars之外进行辅助注册,仍然会发生这种情况。



function derp(strg, maxResidualLength)
    {
      var pattern = /(\b\d{4}.\b|\b\d{4}\b)/g;
      var clippedString = strg.replace(pattern, "");
      if (typeof (maxResidualLength) == "number" && maxResidualLength > 0)
            {
                if (clippedString.length > maxResidualLength);
                {
                    debugger;
                    clippedString = clippedString.substr(0, maxResidualLength) + "&hellip;";
                }
            }
            return clippedString;
    }

    console.log(derp("test", 10)); //hits debugger

最佳答案

这是一件讨厌的小事。我在一个奇怪的地方看到一个;

if (clippedString.length > maxResidualLength); // <-- weird?


简单的复制示例:

if(false); // ";" terminates the if statement.
{
    alert('I get executed anyway...');
}

10-07 14:52