JS library best practice: Return undefined or throw error on bad function input?

前端 未结 3 1121
粉色の甜心
粉色の甜心 2021-01-03 02:24

When coding a library in JavaScript, what is the most standard (friendliest?) way to handle invalid input to a function? My gut tells me that returning undefined is perfectl

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 02:54

    maybe obvious If you extending an environment - continue their practice as a minimum

    Quick answer. If this is javascript in the browser undefined is OK, if it is javascript in the server throw an error.

    Improved Let the library user select the behavior as an option, either library global, or on an object by object basis.

提交回复
热议问题