首页 > 解决方案 > 如果参数是隐式提供的,如何为函数调用提供额外的参数?

问题描述

export const SelectedChoice: (choiceProps) => { ... }

<ChoicePicker {...props}

     onRenderItem={SelectedChoice}

/>

我有这个,隐式提供了参数choiceProps,但是现在我想向第二个参数添加一个函数,以便我可以在SelectedChoice 中调用它,我该怎么做?问题是我不知道传递给 SelectedChoice 的是什么,所以我不能在不破坏函数的情况下显式调用它。如果我知道作为choiceProps 传递的内容,我就不会遇到这个问题。我觉得它被称为回调函数,并且参数在 onRenderItem 中显式提供,但我可能弄错了。

标签: javascriptecmascript-6

解决方案


您也许可以检查arguments隐式存在于函数体中的对象,SelectedChoice如下所示:

export const SelectedChoice: (choiceProps) => {
   if(arguments.length === 1) {
      // do something if no function was passed
   }
   else if(arguments.length === 2 && typeof arguments[1] === "function") {
      // call the passed function
      arguments[1]();
   }
}

推荐阅读