将对象传递给接受参数列表的函数

Passing an Object to a function that accepts a param list

我有一个函数定义如下

postEvent = (data) ->
 throw new Error("category is required for structured events") if not category
 throw new Error("action is required for structured events") if not action

 $window.submit 'type1', data.category, data.action, data.label, data.property, data.value

类别和操作是必需的,但其余的不是。如果 data 对象不包含给定值,则不应传递。

我可以使用大量的 if 语句,但我怀疑有更好的方法。 所以我正在寻找一种方法来解包对象值并传递给其他函数。

示例。

data = {category: 'cat1', action: 'action1', value: 1.2}
postEvent(data)

这应该会产生以下结果

$window.submit 'type1', 'cat1', 'action1', 1.2

您可以使用 destructuring:

postEvent = ({category, action, value}) ->
  console.log category, action, value

postEvent({ category: 'foo', value: 'bar' }) #logs foo, undefined, bar

然后你可以使用call来应用所有参数:

$window.sumbit.call $window, category, action, value

如果您需要验证,我推荐一种不同的方法:

DATA_ORDERING = ['category', 'action', 'value']

postEvent = (data) ->
  let stuff = Object.keys data
    .sort (a, b) -> DATA_ORDERING.indexOf(a) - DATA_ORDERING.indexOf(b)
    .map (k) -> data[k]

  let valid = stuff.every (item) -> item? #make sure non-null

  if valid then $window.submit.apply $window, stuff
  1. 收集需要的参数

    params = [data.category, data.action]
    
  2. 收集所有可选参数

    OPTIONAL = ['label', 'property', 'value']
    for prop in OPTIONAL
      params.push(data[prop]) if data.hasOwnProperty(prop)
    
  3. 使用apply

    调用方法
    $window.submit.apply($window, params)