[英]Why does not Closure type-check the parameters when using function.apply?
見下文
/**
* @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時,確定將哪個參數分配給哪個參數槽是無關緊要的。
如果類型系統增長為元組類型,則應該花更多的精力檢查.apply,因為更可能知道數組“ slot”類型和長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.