美文网首页
javascript类型检测汇总

javascript类型检测汇总

作者: 何家一枝花 | 来源:发表于2021-07-10 14:06 被阅读0次

方法一:typeof +值

#1:数字
typeof 123456
"number" //打印
#2:字符串
typeof 'hello world'
"string"//打印
#3:布尔值
typeof true
"boolean"
#4:数组
typeof [1,2,3,4,5]
"object"
#5:对象
typeof {a:1,functin(){}}
"object"
# null 理解成空对象就可以了
typeof null
"object" 
typeof NaN
"number"
#构造函数 类型都是 object
typeof new Object()
"object"
typeof new String()
"object"
typeof new Boolean()
"object"
  • 对于基本类型,除 null 以外,均可以返回正确的结果。
  • 对于引用类型,除 function 以外,一律返回 object 类型。
  • 对于 null ,返回 object 类型。
  • 对于 function 返回 function 类型。
其中,null 有属于自己的数据类型 Null , 引用类型中的 数组、日期、正则 也都有属于自己的具体类型,而 typeof 对于这些类型的处理,只返回了处于其原型链最顶端的 Object 类型,没有错,但不是我们想要的结果。

方法二:值 +instanceof + 类别 返回 布尔值

var  str = "this is a simple string!"
var constructor_str = new String()
var newStr    = new String("newStr created with constructor");
var myDate    = new Date();
var myObj = new Object();
var myBoolean = new Boolean();
var myNonObj  = Object.create(null);
var arr = new Array(1,2,3)
# string 字符串
str instanceof String //返回 false
constructor_str  instanceof String //返回 true
newStr  instanceof String //返回 true
#对象
myDate   instanceof Object  //返回  true
myDate   instanceof Date  //返回  true
## 我们发现 只要是原型链上的 都是对的

myObj instanceof Object;    // 返回 true, 尽管原型没有定义
myBoolean instanceof Object //返回  true,myBoolean 是通过构造函数生成的
myNonObj instanceof Object; // 返回 false, 一种创建非 Object 实例的对象的方法
({})  instanceof Object; //返回 true
console.log(arr)  // [1, 2, 3]
arr instanceof Object //返回 true

方法三:Object.prototype.toString.apply(值)

toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。
对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
Object.prototype.toString.apply('tr') //"[object String]"
Object.prototype.toString.apply(123) //"[object Number]"
Object.prototype.toString.apply(true) //"[object Boolean]"
Object.prototype.toString.apply(null) //"[object Null]"
Object.prototype.toString.apply(NaN) //"[object Number]"
Object.prototype.toString.apply(undefined) //"[object Undefined]"
Object.prototype.toString.apply([1,2,3]) //"[object Array]"
Object.prototype.toString.apply({}) //"[object Object]"

Object.prototype.toString.apply(new Date()) //"[object Date]"
Object.prototype.toString.apply(new Boolean())//"[object Boolean]"
Object.prototype.toString.apply(new String()) //"[object String]"
Object.prototype.toString.apply(new Object())//"[object Object]"

Object.prototype.toString.apply(new Set()) // "[object Set]"
Object.prototype.toString.apply(new WeakSet()) //"[object WeakSet]"

Object.prototype.toString.apply(new WeakMap()) //"[object WeakMap]"
Object.prototype.toString.apply(new Map())  //"[object Map]"

Object.prototype.toString.apply(Symbol) //"[object Function]"
Object.prototype.toString.apply(function(){}) //"[object Function]"

Object.prototype.toString.apply(new Error()) //"[object Error]"
Object.prototype.toString.apply(new RegExp()) //"[object RegExp]"

方法四:constructor

'hello'.constructor == String // 返回  true
 false.constructor == Boolean;           //返回  true
"123".constructor == String;            // 返回 true
new Number(123).constructor == Number;  //返回  true
[].constructor == Array;                // 返回 true
new Function().constructor == Function; //返回  true
new Date().constructor == Date;         //返回  true
document.constructor == HTMLDocument;   //返回  true
window.constructor == Window;           //返回  true
new Error().constructor == Error;       //返回  true

相关文章

  • javascript类型检测汇总

    方法一:typeof +值 对于基本类型,除 null 以外,均可以返回正确的结果。 对于引用类型,除 funct...

  • JavaScript 类型检测

    JavaScript 类型检测 本文介绍JavaScript的几种类型检测的方法,以及其各自的适用范围。 Java...

  • Javascript 数据类型检测及原理

    前言 数据类型检测是 JavaScript 中既基础又考验原理的知识,如果你对 JavaScript 数据类型检测...

  • JavaScript类型检测

    typeof 适合基本类型和函数类型,遇到null失效 instanceof 判断左边的原型链上是否有右边构造函数...

  • JavaScript类型检测

    本篇介绍一下如何检测JavaScript各种类型。 5种原始类型 对象 Function Array 属性 5种原...

  • JavaScript类型检测

    参考视频:JavaScript类型检测-慕课网 可以用以下5种来进行类型检测 typeof 适用场景: 检测基础类...

  • JavaScript类型检测

    既然是一个弱类型的语言,那么如何检测类型呢? 1.typeof typeof会返回一个基础数据类型或对象的字符串表...

  • Javascript类型检测

    typeof适合基本类型及function检测,遇到null失效。 [[class]]通过{}.toString拿...

  • JavaScript基础知识(一)

    尝试汇总下JavaScript的基础知识 本文包括: JavaScript简介(1) 标签(2) 数据类型(3) ...

  • javascript检测变量的类型

    javascript检测变量的类型:目前JavaScript有三种方式:typeof,instanceof,obj...

网友评论

      本文标题:javascript类型检测汇总

      本文链接:https://www.haomeiwen.com/subject/ieycpltx.html