Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

study hard and make progress every day

preface

In the process of business development, we must not avoid a problem, is the judgment of data type. Today we are going to talk about common data type judgments.

typeof

Typeof determines the basic data type and returns a string. The return value has the following possibilities:

  • ‘undefined’
  • ‘boolean’
  • ‘string’
  • ‘number’
  • ‘symbol’
  • ‘bigint’
  • ‘function’
  • ‘object’

Can’t determine a more specific Object types, so generally is combined with the Object. The prototype. ToString use.

constructor

Constructor, so you can determine specific objects and functions.

For example, check arrays and functions like this, just for example.

function isArray(arr){
    return!!!!! (arr && arr.constructor ===Array)}function isFunction(fn){
    return!!!!! (fn && fn.constructor ===Function)}Copy the code

Note, however, that constructors can be overwritten, and of course you can make them unmodificable by using Object.freeze or Object.defineProperty.

Take a look at Redux’s [kindof.js] (github.com/reduxjs/red…)

     const constructorName = ctorName(val)
      switch (constructorName) {
        case 'Symbol':
        case 'Promise':
        case 'WeakMap':
        case 'WeakSet':
        case 'Map':
        case 'Set':
          return constructorName
        default:
          break
      }
Copy the code

instanceof

The idea is to look up the prototype chain, which is an instance, but the prototype chain is another topic.

function isArray(arr){
    return!!!!! (arr && arrinstanceof Array)}Copy the code

Take a look at the isError method of Redux’s kindOf. Js

    function isError(val) {
      return (
        val instanceof Error| | -typeof val.message === 'string' &&
          val.constructor &&
          typeof val.constructor.stackTraceLimit === 'number'))}Copy the code

isPrototypeOf

It’s also essentially a prototype chain judgment. Fetch has a piece of code for DataView:

function isDataView(obj) {
  return obj && DataView.prototype.isPrototypeOf(obj)
}
Copy the code

Object.prototype.toString

This and Typeof are the most common methods of determining data types.

const toString = Array.prototype.toString
function isWindow(obj){
    return toString.call(obj) == "[object Window]"
}
Copy the code

Duck type detection

Check whether certain properties and methods exist.

A million downloads a week of the P-IS-Promise

const isObject = value= >value ! = =null &&
	(typeof value === 'object' || typeof value === 'function');

export default function isPromise(value) {
	return value instanceof Promise ||
		(
			isObject(value) &&
			typeof value.then === 'function' &&
			typeof value.catch === 'function'
		);
}
Copy the code

Before array. isArray, Array detection was also duck type detection. It’s all about length and subscript.

Symbol.toStringTag

This is one way that ES6+ provides.

MDN description:

Symbol.toStringTag is a built-in Symbol that is usually used as the attribute key of an object. The corresponding attribute value should be a string representing the object’s custom type tag. Usually only built-in Object. The prototype. The toString () method to read the label and put it in his own return values.

window[Symbol.toStringTag] // Window
(new Set(to))Symbol.toStringTag] // Set
Copy the code

Such as comparative

Of course, direct comparison, such as whether window.

function isCurrentWindow(obj){
    return obj === window
}
Copy the code

Like underscode’s judgment of undefined.

  // Is a given variable undefined?
  _.isUndefined = function(obj) {
    return obj === void 0;
  };
Copy the code

summary

Did you harvest anything today?