JavaScript style for optional callbacks

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

    A valid function is based on the Function prototype, use:

    if (callback instanceof Function)
    

    to be sure the callback is a function

提交回复
热议问题