Javascript 为什么在使用function.apply时闭包类型不检查参数?
见下文Javascript 为什么在使用function.apply时闭包类型不检查参数?,javascript,google-closure-compiler,google-closure,Javascript,Google Closure Compiler,Google Closure,见下文 /** * @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]); //
/**
* @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
}
为什么闭包仅在使用call而不应用时才会引发错误?有没有一种方法可以让闭包类型在使用apply时检查参数?因为(a)类型检查器还没有元组类型的概念,(b)很少使用数组文本调用方法。当使用.call确定将哪个参数分配给哪个参数槽时,哪个参数槽是无关紧要的
如果类型系统增加了一个元组类型,那么在检查上投入更多的精力是有意义的。应用,因为数组的“槽”类型和长度更可能是已知的。你能显式地键入数组吗?@Bergi如果有办法,我不知道。