JS数据类型--数据类型判断(二)

写在前面
上一篇我已经介绍了js的集中基本数据类型,但是引用类型又包括Array、Object等,这章我来说一下如何判断一个变量的数据类型。

一、数据类型

基本数据类型:String Boolean Number Symbol(ES6新增) Undefined Null
复杂数据类型:Object
Symbol(ECMAScript 6 新定义数据类型)暂时不涉及
基本数据类型中有两个为特殊数据类型: null undefined
复杂数据类型:Array Math Date RegExp Function Object Error

二、判断数据类型 typeof

typeof操作符是检测基本类型的最佳工具

返回结果 含义
undefined 未定义
boolean 布尔值
string 字符串
number 数值
object 对象或null
function 函数

对未初始化的变量执行typeof操作符会返回undefined值,而对未声明的变量执行typeof操作符同样也会返回undefined

var test; // 这个变量声明之后默认取得了 undefined 值
// var age // 下面这个变量并没有声明
console.log(typeof test); // "undefined"
console.log(typeof age); // "undefined"

从逻辑角度来看,null值表示一个空对象指针,而这也正是使用 typeof 操作符检测null值时会返回object的原因。此外,函数类型可返回function
注意首字母为小写字母 例如: typeof 'abc'=== 'string'

三、判断数据类型 Instanceof

instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上

所以instanceof可以用于判断typeof返回object的引用类型存储值的类型
JavaScript 中一切皆对象,如下代码第3行返回false,其实问题的关键在于typeof str返回string并不是object

var str = "This is a simple string"; 
var newStr    = new String("String created with constructor");
str instanceof String; // 返回 false, 检查原型链会找到 undefined 
newStr instanceof String; // 返回 true

这样 Boolean Number是一样的逻辑。还剩下两种基本类型:Null Undefined
undefined 当我们对变量只声明没有初始化时,输出为 undefined,typeof undefined 返回的是 undefined 也不是 object 类型,所以 undefined 并不是任何对象的实例。

null 表示的是空对象,虽然 typeof null 是 object,但是 null 和 undefined 一样并没有任何属性和方法,在 instanceof 定义中也有判断,如果类型不是 object(这个类型判断并不是跟 typeof 返回值一样),就返回 false。


JS数据类型--数据类型判断(二)_第1张图片
Null Undefined数据类型instanceof判断

使用 instanceof 就是确定原型和实例之间的关系

var Fn = function() {};
var f1 = new Fn();
console.log(f1 instanceof Fn); // true
console.log(f1 instanceof Function ); // false
console.log(f1 instanceof Object ); // true

instanceof 还可以在继承关系中用来判断一个实例是否属于它的父类型

// 判断 f2 是否是 Fn2 类的实例 , 并且是否是其父类型的实例
function An2(){} 
function Fn2(){} 
Fn2.prototype = new An2(); // JavaScript 原型继承
 var f2= new Fn2(); 
console.log(f2 instanceof Fn2); // true 
console.log(f2 instanceof An2); // true
JavaScript instanceof 运算符代码
function instance_of(L, R) {//L 表示左表达式,R 表示右表达式
 var O = R.prototype;// 取 R 的显示原型
 L = L.__proto__;// 取 L 的隐式原型
 while (true) { 
   if (L === null) 
     return false; 
   if (O === L)// 这里重点:当 O 严格等于 L 时,返回 true 
     return true; 
   L = L.__proto__; 
 } 
}

更多instanceof 内容请查看博客【JavaScript instanceof 操作符】

四、判断数据类型 constructor

constructor 属性返回对创建此对象的数组函数的引用。

object.constructor 其中object 是一个对象或函数的名称。
constructor 属性是每个具有原型的对象的原型成员。 这包括除 Global 和 Math 对象之外的所有内部 JavaScript 对象。

'abc'.constructor == String; // true
(123).constructor == Number; // true
(true).constructor == Boolean; // true
[1,2].constructor == Array; // true
({name:'yuhoo'}).constructor == Object; // true
(function(){}).constructor == Function; // true
(new Date()).constructor == Date; // true
(/[a-z]/).constructor == RegExp; // true
JS数据类型--数据类型判断(二)_第2张图片
Null Undefined数据类型constructor判断

下面是使用new创建的基础数据类型

var str = new String('abc');
str.constructor === String; // true

在继承关系中用来判断会出现错误

function An2(){} 
function Fn2(){} 
Fn2.prototype = new An2(); // JavaScript 原型继承
var f2 = new Fn2(); 
console.log(f2.constructor === Fn2); // false 
console.log(f2.constructor === An2); // true

五、判断数据类型 Object.prototype.toString.call(obj)

Object.prototype.toString.call() 最准确最常用的方式。首先获取Object原型上的toString方法,让方法执行,让toString方法中的this指向第一个参数的值。

Object.prototype.toString.call(undefined) ; // "[object Undefined]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(123); // "[object Number]"
Object.prototype.toString.call('abc'); // "[object String]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call(new Function()); // "[object Function]"
Object.prototype.toString.call(new Date()); // "[object Date]"
Object.prototype.toString.call(new RegExp()); // "[object RegExp]"
Object.prototype.toString.call(new Error()); // "[object Error]"
Object.prototype.toString.call(document); // "[object HTMLDocument]"
Object.prototype.toString.call(window); // "[object Window]" window是全局对象global的引用
// 使用new创建的基础数据类型对象
var str = new String();
Object.prototype.toString.call(str); // "[object String]"

更多内容查看【JavaScript:Object.prototype.toString方法的原理】

六、其他

判断null数据类型

x === null

总结

我还不是很理解原型链,等之后补充完整(各个功能的区别)

你可能感兴趣的:(JS数据类型--数据类型判断(二))