JavaScript style for optional callbacks

后端 未结 10 1724
日久生厌
日久生厌 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:59

    Simply do

    if (callback) callback();
    

    I prefer to call the callback if supplied, no matter what type it is. Don't let it fail silently, so the implementor knows he passed in an incorrect argument and can fix it.

提交回复
热议问题