JS accurate determination of how to determine the type of data (an array of objects)

Analyzing the data type using Object.prototype.toString

Common way to judge a variety of deficiencies, typeof not say, fuzzy judgment

This way you can judge constructor, but there are shortcomings, if aaa is null or undefined when the code will error

var aaa = {}
aaa.constructor === Object //true

Here's a perfect determination method: using Object.prototype.toString.call () Analyzing

var toString = Object.prototype.toString;
  
  function isArray(val) {
    return toString.call(val) === '[object Array]';  
  }
  
  function isObject(val) {
    return toString.call(val) === '[object Object]';
  }
  
  function isNull(val) {
    return toString.call(val) === '[object Null]';  
  }
  
  function isUndefined(val) {
    return toString.call(val) === '[object Undefined]';
  }

So, that is able to determine accurately, but does not complain, is not perfect, welcome Comments

Guess you like

Origin www.cnblogs.com/zhujunislucky/p/12059096.html