JavaScript 数据类型判断

数据类型

JavaScript的数据类型分为基本数据类型和引用数据类型两种
基本数据类型

  1. NUmber
  2. String
  3. Boolean
  4. undefined
  5. Symbol
  6. null

引用数据类型

  1. Object
    • Object包含function,Array,Date等。

一、typeof

typeof用来判断基本数据类型,返回值是数据类型

  • typeof只能用来判断基本数据类型,对于引用数据类型来说只会返回一个Object(function会返回function)。
    在 JavaScript 中二进制前三位都为 0 的话会被判
    断为 object 类型, null 的二进制表示是全 0,自然前三位也是 0,所以执行 typeof 时会返回“ object ”
  • typeof判断的写法有两种
  • typeof XXX或者typeof(XXX)
function fun() {

}
console.log(typeof fun);          // function
console.log(typeof {});           // object
console.log(typeof null);         // object
console.log(typeof undefined);    // undefined
console.log(typeof "fun");        // string
console.log(typeof 0);            // number
console.log(typeof new Date());   // object
console.log(typeof []);           // object
console.log(typeof NaN);          // number

instanceof

instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。第一个参数是要检测的对象 第二是另一个构造函数。instanceof判断的是原型,返回的数true或者false

var a = 1
var b = "1"
var c
var d = {}
var e = []
var f = new Date()
var g = undefined
var h = NaN
var i = function test(){

}

console.log(a instanceof Number);     // false
console.log(b instanceof String);     // false
console.log(c instanceof Object);     // false
console.log(d instanceof Object);     // true
console.log(e instanceof Object);     // true
console.log(f instanceof Object);     // true
console.log(g instanceof Object);     // false
console.log(h instanceof Object);     // fasle
console.log(i instanceof Object);     // true

instanceof一般用来检测原型

function Foo(){

}
var foo = new Foo()
// foo的原型链中是否存在Foo的原型
console.log(foo instanceof Foo);          // true

instanceof 复杂用法

console.log(Object instanceof Object);              // true
//第一个Object的原型链:Object=>
//Object.__proto__ => Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个Object的原型:Object=> Object.prototype
console.log(Function instanceof Function);          // true
//第一个Function的原型链:Function=>Function.__proto__ => Function.prototype
//第二个Function的原型:Function=>Function.prototype
console.log(Function instanceof Object);            // true
//Function=>
//Function.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//Object => Object.prototype
console.log(String instanceof String);              // false
//第一个String的原型链:String=>
//String.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个String的原型链:String=>String.prototype
console.log(Boolean instanceof Boolean);            // false
//第一个Boolean的原型链:Boolean=>
//Boolean.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
//第二个Boolean的原型链:Boolean=>Boolean.prototype

constructor

constructor是prototype对象上的属性,指向构造函数。当函数被定义时候,js引擎会为函数添加原型prototype,并且这个prototype中constructor属性指向函数引用, 因此重写prototype会丢失原来的constructor。

除了undefined和null之外,其他类型都可以通过constructor属性来判断类型,但是如果把一个构造函数的原型指向改变了
也判断不了。

console.log("".constructor == String);                  // true
console.log(new Number(1).constructor == Number);       // true
console.log(true.constructor == Boolean);               // true
console.log(Function().constructor == Function);        // true
console.log(new Date().constructor == Date);            // true

Object.prototype.toString.call

使用Object.prototype.toString.call()的方式来判断一个变量的类型是最准确的方法

var judgeType = Object.prototype.toString
console.log(judgeType.call(""));                // [object String]
console.log(judgeType.call(1));                 // [object Number]
console.log(judgeType.call(true));              // [object Boolean]
console.log(judgeType.call(Function));          // [object Function]
console.log(judgeType.call(new Date()));        // [object Date]
console.log(judgeType.call({}));                // [object Object]
console.log(judgeType.call([]));                // [object Array]
console.log(judgeType.call(undefined));         // [object Undefined]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值