Javascript类型检查我做对了吗?

-(已编辑)此问题不仅适用于数字,而是适用于所有类型。我正在使用类似的东西:


exampleFunction1(a,b){

   if(

        Object.prototype.toString.call(a) === "[object Number]" &&

        Object.prototype.toString.call(b) === "[object Number]"

     ){

        return a+b;

     }else{

        console.log('%cType Error : exampleFunction1(<Number>)','color:#00FF66;')

     }

}


exampleFunction2(type,length,format) {

        if(

            Object.prototype.toString.call(type) === "[object String]" &&

            Object.prototype.toString.call(length) === "[object Number]" &&

            (Object.prototype.toString.call(format) === "[object String]" || Object.prototype.toString.call(format) === "[object Undefined]" )

        ){


        }else{

            console.log("%cType Error : exampleFunction2(<String>,<Number>,<String>)","color:#00FF66;")

        }

    }

在我几乎所有的函数中,在开始编写我的实际代码之前,都要严格检查其输入类型。它主要是关于我将在我的团队和我自己的图书馆中共享的功能,其他人将尝试使用。这被认为是一种好的做法还是没有必要?


慕码人8056858
浏览 100回答 3
3回答

白板的微信

你不会找到一个正确的答案,但你可以找到强烈的意见。我的观点是:它各不相同。以下是我的库中的一些类型检查const isDefined = x => x !== undefined && x !== nullconst isUndefined = x => x === undefinedconst isNull = x => x === nullconst isIterable = x => isDefined(x) && isDefined(x[Symbol.iterator])const isAsyncIterable = x => isDefined(x) && isDefined(x[Symbol.asyncIterator])const isReadable = x => x && typeof x.read === 'function'const isWritable = x => x && typeof x.write === 'function'const isFunction = x => typeof x === 'function'const isArray = Array.isArrayconst numberTypedArrays = new Set([&nbsp; 'Uint8ClampedArray',&nbsp; 'Uint8Array', 'Int8Array',&nbsp; 'Uint16Array', 'Int16Array',&nbsp; 'Uint32Array', 'Int32Array',&nbsp; 'Float32Array', 'Float64Array',])const isNumberTypedArray = x => x && x.constructor && (&nbsp; numberTypedArrays.has(x.constructor.name))const bigIntTypedArrays = new Set([&nbsp; 'BigUint64Array', 'BigInt64Array',])const isBigIntTypedArray = x => x && x.constructor && (&nbsp; bigIntTypedArrays.has(x.constructor.name))const isNumber = x => typeof x === 'number'const isBigInt = x => typeof x === 'bigint'const isString = x => typeof x === 'string'const isPromise = x => x && typeof x.then === 'function'const is = fn => x => x && x.constructor === fnconst isObject = is(Object) // checks directly for Object, isObject([]) === false如果您正在寻找一种快速方法来检查给定构造函数的类型,我建议复制和粘贴并使用它,以便isconst is = fn => x => x && x.constructor === fnexampleFunction1(a,b){&nbsp; &nbsp;if(is(Number)(a) && is(Number)(b)){&nbsp; &nbsp; &nbsp; &nbsp; return a+b;&nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; console.log('%cType Error : exampleFunction1(<Number>)','color:#00FF66;')&nbsp; &nbsp; &nbsp;}}打字就更少了。

慕雪6442864

您应该使用方法检查是否为数字isNaNaddNumbers(a,b){&nbsp; &nbsp;if(&nbsp; &nbsp; &nbsp; &nbsp; !isNaN(a) &&&nbsp; &nbsp; &nbsp; &nbsp; !isNaN(b)&nbsp; &nbsp; &nbsp;){&nbsp; &nbsp; &nbsp; &nbsp; return a+b;&nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; console.log('%cType Error : findUniqueInArray(<Array>)','color:#00FF66;')&nbsp; &nbsp; &nbsp;}}

哔哔one

依赖似乎不是最安全的选择,因为任何对象都可以实现自己的.toString()toString()我会去:Number.isFinite()if (Number.isFinite(a) && Number.isFinite(b)) {如果您只想检查类型,则有一个运算符:typeoftypeof 1 // "number"typeof 'a' // "string"typeof {} // "object"至于,库经常实现自己的来帮助你调试。以下是使用类完成操作的方式:toString()toString()class Foo {&nbsp; &nbsp; toString() {&nbsp; &nbsp; &nbsp; &nbsp; return 'I am mr. Foo';&nbsp; &nbsp; &nbsp;}}const bar = new Foo();bar.toString() // "I am mr. Foo"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript