gpt4 book ai didi

javascript - 为什么 Closure 在使用 function.apply 时不对参数进行类型检查?

转载 作者:数据小太阳 更新时间:2023-10-29 05:33:15 24 4
gpt4 key购买 nike

见下文

/**
* @param {string} a
* @param {string} b
*/
var f = function(a, b){
// ...
}

/**
* @param {string} a
* @param {boolean} c
*/
var h = function(a, c){
f.apply(this, arguments); // no compile error
f.apply(this, [a, c]); // no compile error
f.call(this, a, c); // compile error: does not match formal parameter
}

为什么 Closure 只在使用 call 而不是 apply 时报错?
有没有一种方法可以在我使用 apply 时对参数进行闭包类型检查?

最佳答案

因为 (a) 类型检查器还没有元组类型的概念,并且 (b) 很少用数组字面量调用方法。使用 .call 时,确定将哪个参数分配给哪个参数槽是微不足道的。

如果类型系统增长了一个元组类型,那么投入更多精力检查 .apply 是有意义的,因为数组“槽”类型和长度更有可能是已知的。

关于javascript - 为什么 Closure 在使用 function.apply 时不对参数进行类型检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933870/

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