简体   繁体   English

为什么在使用function.apply时Closure不进行类型检查?

[英]Why does not Closure type-check the parameters when using function.apply?

See below 见下文

/**
 * @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
}

Why does Closure raise an error only when using call and not apply? 为什么闭包仅在使用call时不产生错误?
Is there a way I can made closure type-check the parameters even when I'm using apply? 有没有一种方法可以使闭包在使用Apply时进行类型检查?

Because (a) the type checker doesn't yet have the concept of an tuple type and (b) it is rare to call a method with an array literal. 因为(a)类型检查器还没有元组类型的概念,并且(b)很少用数组文字调用方法。 When using .call determining which argument is assigned to which parameter slot is trivial. 使用.call时,确定将哪个参数分配给哪个参数槽是无关紧要的。

If the type system grows a tuple type, it would make sense to put more effort into checking .apply as the array "slot" types and length is more likely to be known. 如果类型系统增长为元组类型,则应该花更多的精力检查.apply,因为更可能知道数组“ slot”类型和长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM