javascript类型检测汇总

2021-07-10  本文已影响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 有属于自己的数据类型 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
上一篇 下一篇

猜你喜欢

热点阅读