【问题标题】:handlebars / function ends up in impossible condition车把/功能最终处于不可能的状态
【发布时间】:2015-09-24 08:58:24
【问题描述】:

我有以下车把助手,在打字稿文件中定义。

当为帮助程序提供字符串"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 ,如果我在打字稿和车把之外进行辅助注册,它仍然会发生。

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

【问题讨论】:

    标签: javascript typescript handlebars.js


    【解决方案1】:

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

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

    简单再现示例:

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

    【讨论】:

    • 啊!你找到了。不敢相信我错过了
    猜你喜欢
    • 2022-01-07
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    相关资源
    最近更新 更多