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

前端 未结 3 1114
粉色の甜心
粉色の甜心 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:55

    I think undefined is fine but keep in mind that:

    JavaScript does not have a void type, so every function must return a value. The default value is undefined, except for constructors, where the default return value is this.

    So you don't need to explicitly returns undefined. It will be by default.

    see http://javascript.crockford.com/survey.html

提交回复
热议问题