gpt4 book ai didi

javascript - Handlebars /功能最终处于不可能的状态

转载 作者:行者123 更新时间:2023-12-02 15:38:18 24 4
gpt4 key购买 nike

我有以下 Handlebars 助手,在 typescript 文件中定义。

当助手输入字符串 "test" 时,且 maxResidualLength 为 10 ,尽管检查员告诉我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...');
}

关于javascript - Handlebars /功能最终处于不可能的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com