Code Monkey home page Code Monkey logo

Comments (2)

GenXiaoLe avatar GenXiaoLe commented on June 22, 2024
var arr = [];
var obj = {};
var date = new Date();
var fun = function() {};
var num = 0;
var str = '';
var n = null;
var nan = NaN;
var bool = true;
var un = undefined;

// 类型集合
var _type = [arr, obj, date, num, str, n, fun, nan, bool, un];

function printType(result) {
  for(var i = 0; i < result.length; i++) {
    var _item = result[i];

    if (typeof(_item) == 'number' && isNaN(_item)) {
      console.log('[object NaN]');
      continue;
    }
    console.log(Object.prototype.toString.call(_item))
  }
}
printType(_type);

PS: 常用的对象类型判断方法中,Object.prototype.toString.call()是我目前接触过判断类型最准确的方法,只有 NaN 会判断为number,特殊处理一下就好。其它方式中,typeof NaN 和 numer 均会判断为 number,[] , {}, Date 和 null等均会判断为 objectinstanceof返回类型为boolean,单个判断某对象是否为某类型, 个人认为可用于针对某种类型去判断,不太适合运用在集成判断类型的方法中。

from daily.

MMmaXingXing avatar MMmaXingXing commented on June 22, 2024
// 判断对象类型
// 测试用例
var arr = [];
var obj = {};
var date = new Date();
var fun = function() {};
var num = 0;
var str = "";
var n = null;
var nan = NaN;
var bool = true;
var un = undefined;

// 类型集合
var _type = [arr, obj, date, num, str, n, fun, nan, bool, un];

function getType(value) {
    if (typeof value == 'number' && isNaN(value)) {
        return "[object NaN]";
    }
    return Object.prototype.toString.call(value);
}

function getObjType(obj) {
    var typeList = [];
    for(var i = 0; i < obj.length; i++) {
        var itemType = getType(obj[i]);
        typeList.push({ item: obj[i], type: itemType });
    }
    return typeList;
}

console.log(getObjType(_type));

from daily.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.