0

有没有办法(希望干净)我可以在这里做同样的事情而不分配状态值?当 if 条件在同一行时,我一直遇到 if 条件返回布尔值(而不是函数)的问题。

onItemChanged = (event,ui) ->
  input_element = event.target
  list_id = input_element.getAttribute('list_id')
  state = if input_element.checked
            onItemChecked //function of int -> nothing
          else
            onItemUnchecked //function of int -> nothing

  state (list_id )
4

2 回答 2

2

像这样:

onItemChanged = (event, ui) ->
  input = event.target
  state = if input.checked then onItemChecked else onItemUnchecked
  state input.getAttribute('list_id')

甚至:

onItemChanged = ({target: input}, ui) ->
  state = if input.checked then onItemChecked else onItemUnchecked
  state input.getAttribute('list_id')
于 2012-11-15T16:58:53.050 回答
1

不确定您是否认为它更干净,但您也可以if直接调用表达式的结果:

onItemChanged = (event,ui) ->
  input_element = event.target
  list_id = input_element.getAttribute('list_id')
  (if input_element.checked then onItemChecked else onItemUnchecked) list_id

或者也许只是在里面调用onItemCheckedor :onItemUncheckedif

onItemChanged = (event,ui) ->
  input_element = event.target
  list_id = input_element.getAttribute('list_id')
  if input_element.checked 
    onItemChecked list_id
  else 
    onItemUnchecked list_id
于 2012-11-15T16:58:34.493 回答