js中判断数据类型都知道用tyoeof(),但是他只能判断object,boolean,function,string,number,undefined还有symbol(es6新增)这几种初步类型,比如new Date和null,它就只能是object。

console.log(typeof(new Date())); // object

console.log(typeof(null));// object

console.log(typeof([1,2,3]));// object

那么,我们使用Object.prototype.toString.call(),就可以做到

console.log(Object.prototype.toString.call("jerry"));//[object String]

console.log(Object.prototype.toString.call(12));//[object Number]

console.log(Object.prototype.toString.call(true));//[object Boolean]

console.log(Object.prototype.toString.call(undefined));//[object Undefined]

console.log(Object.prototype.toString.call(null));//[object Null]

console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]

console.log(Object.prototype.toString.call(function(){}));//[object Function]

console.log(Object.prototype.toString.call([]));//[object Array]

console.log(Object.prototype.toString.call(new Date));//[object Date]

console.log(Object.prototype.toString.call(/\d/));//[object RegExp]

function Person(){}; console.log(Object.prototype.toString.call(new Person));//[object Object]

判断console.log(Object.prototype.toString.call(new Person) === '[object Object]'); //true

Object.prototype.toString.call()和typeof()区别的更多相关文章

  1. typeof 和 Object.prototype.toString.call 数据类型判断的区别

    使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...

  2. instanceof, typeof, & Object.prototype.toString

    /** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...

  3. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  4. Object.prototype.toString & typeof

    Object.prototype.toString & typeof Object.prototype.toString 获取某个对象属于哪种内置类型 typeof  得到某个对象的类型 差别 ...

  5. instance of type of object.prototype.tostring 区别

    typeof typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型.   返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 6 种:   number.boolea ...

  6. 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()

    1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...

  7. JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()

    1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...

  8. typeof 、Object.prototype.toString和 instanceof

    数据类型 js 基本类型包括:Undefined  symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...

  9. typeof操作符,返回数据类型Array.isArray()、Object.prototype.toString.call()

    源地址https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof typeof操作符 // N ...

随机推荐

  1. PHP-Parse 简介以及在 Hyperf 中的应用

    介绍 PHP-Parse 是分析 PHP 代码生成 AST 的库,分析出可读性很高的对象数据结构,方便后续的更新和遍历. PHP-Parse 的主要作用是修改原有代码(比如插入自定义的代码片段),生成 ...

  2. 手把手教你使用Vuex(四)

    3.Action Action类似于mutation,不同之处在于: Action提交的是mutation,而不是直接变更状态 Action可以包含任何异步操作 可以理解为将mutations里面处理 ...

  3. Tomcat Web服务器与常用Web服务器

    一.常用Web服务器 Tomcat ​ 由Apache组织提供的一种Web服务器,提供对jsp和servlet的支持.它是一种轻量级的javaWeb容器服务器.也是当前应用最广的JavaWeb服务器( ...

  4. Centos7下Jewel版本radosgw服务启动

    前言 本篇介绍了centos7下jewel版本的radosgw配置,这里的配置是指将服务能够正常起来,不涉及到S3的配置,以及其他的更多的配置,radosgw后面的gw就是gateway的意思,也就是 ...

  5. 学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片

    目录 第一部分:Consul 基础 1,Consul 介绍 2,安装 Consul Ubuntu/Debian 系统 Centos/RHEL 系统 检查安装 3,运行 Consul Agent 启动 ...

  6. [head first 设计模式] 第一章 策略模式

    [head first 设计模式] 第一章 策略模式 让我们先从一个简单的鸭子模拟器开始讲起. 假设有个简单的鸭子模拟器,游戏中会出现各种鸭子,此系统的原始设计如下,设计了一个鸭子超类,并让各种鸭子继 ...

  7. Struts2漏洞渗透笔记

    虽然这是一次失败的渗透,但是也学到不少新姿势. 目标机环境:①外网②win2012③360全家桶 一,利用Struts2终极利用工具上传一句话 jsp的一句话,有时候会出现兼容问题.比如Cknife的 ...

  8. linux学习笔记全-如何学习linux?

    简介 今天整理文件整理出了好多年前学习linux的笔记 就整理下发布在博客上怕文件形式会误删. linux入门基础对于新手而言不推荐看书!!不推荐看书!!(大牛跳过)先看视频看linuxcast的视频 ...

  9. JVM学习之Java技术体系

    目录 一.Java技术体系 1.Java体系构成 2.JDK.JRE.JVM之前的关系 JVM介绍 (1)JVM官方文档定义 (2)中文解释 JVM结构 Java代码执行流程 JVM架构模型 1.指令 ...

  10. java8新特性LocalDate、LocalTime、LocalDateTime的学习

    以前操作时间都是使用SimpleDateFormat类改变Date的时间格式,使用Calendar类操作时间.但是SimpleDateFormat是线程不安全的,源码如下: private Strin ...