【问题标题】:The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consider using a standard function expression'arguments' 对象不能在 ES3 和 ES5 的箭头函数中被引用。考虑使用标准函数表达式
【发布时间】:2020-07-20 05:04:01
【问题描述】:

目前正在构建一个示例白板项目并面临打字稿问题

var format = (formatString, ...params: any[]): string => {
    var i = 0;

    while (/%s/.test(formatString)) {
        formatString = formatString.replace('%s', arguments[++i])  --> Error at (arguments)

    }
    return formatString;
};

我检查了堆栈溢出的类似问题,但没有得到具体答案

【问题讨论】:

  • 请帮帮我
  • 你的.tsconfiges 目标是什么?

标签: asp.net-mvc typescript arrow-functions


【解决方案1】:

您对 JS 和 typescript 中剩余参数功能的可用性感到困惑。

arguments 对象用于 ES3 和 ES5 中参数数量可变的函数这一事实不应该让你在 typescript 中使用它。

对于 typescript 中参数数量可变的函数,您应该使用rest parameters。 Typescript 编译器会将其编译为:

var format = (formatString: string, ...params: any[]): string => {
    var i = 0;

    while (/%s/.test(formatString)) {
        formatString = formatString.replace('%s', params[i++]);

    }
    return formatString;
};

Playground Link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 2012-09-30
    • 2019-08-13
    • 2019-03-05
    • 2015-02-26
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多