JavaScript style for optional callbacks

后端 未结 10 1723
日久生厌
日久生厌 2020-12-02 07:30

I have some functions which occasionally (not always) will receive a callback and run it. Is checking if the callback is defined/function a good style or is there a better w

10条回答
  •  盖世英雄少女心
    2020-12-02 07:47

    I have sinced moved to coffee-script and found default arguments is a nice way to solve this problem

    doSomething = (arg1, arg2, callback = ()->)->
        callback()
    

提交回复
热议问题