1
函數我有定義爲函數如下傳遞對象到接受PARAM列表
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