What is a good unique way to type check?

One simple approach used in libraries such as trunk:

typeof o === type; 

ES5 suggests using something similar to

 (Object.prototype.toString.call(o) === '[object ' + type + ']'); 

and finally, SO users shared: ( getType code overview )

 o.constructor === type; 

Here are three fundamentally different ways.

  • using typeof
  • using toString
  • using constructor property

I would like to use these 3 methods to write a generic checkType method.

Any advice on how to best combine them logically or why is there can be in many ways.

+4
source share
2 answers
0
source

Source: https://habr.com/ru/post/1469525/


All Articles