How to check if type is Boolean

前端 未结 16 1795
深忆病人
深忆病人 2020-12-07 10:09

How can I check if a variable\'s type is of type Boolean?

I mean, there are some alternatives such as:

if(jQuery.type(new Boolean()) === jQuery.type(         


        
16条回答
  •  鱼传尺愫
    2020-12-07 10:38

    Creating functions like isBoolean which contains oneliner typeof v === "boolean" seems very unhandy in long term. i am suprised that almost everyone suggest to create your own function. It seems to be same cancer as extending native prototypes.

    • you need to recreate them in every project you are involved
    • other developers might have different habits,, or need to check source of your function to see which impementation of check you use, to know what are weak points of your check
    • you will be fruustrated when you will try to write one liner in console on site which doesn't belong to your project

    just memoize typeof v === "boolean" and that's all. Add a template to your IDE to be able to put it by some three letter shortcut and be happy.

提交回复
热议问题