javascript数据类型判断

时间:2023-03-08 23:55:37

javascript基本数据类型:原始类型  和  引用类型

原始类型(简单数据类型):String,Number,Boolean,Undefined,Null,Symbol(ES6新增)

引用类型(复杂数据类型):Array,Object

 var a = 'hello world';
var b = 100;
var c;
var d = null;
var e = {
name : 'My name is Pelli.'
}
var f = [100,3,'hello',[4,4,67,'hah'],{age : 34}];
var g = function(){
console.log("I am Pelli.");
} console.log(typeof a);//'string'
console.log(typeof b);//'number'
console.log(typeof c);//'undefined'
console.log(typeof d);//'object'
console.log(typeof e);//'object'
console.log(typeof f);//'object'
console.log(typeof g);//'function'

关键方法:typeof,instanceof,constructor,prototype

关键点:

  1. typeof判断的结果,返回值肯定是字符串
  2. typeof判断的结果的返回值一定在以下六个之中,ES6(也称为ES2015)之后新增一个,一共七个
  3. typeof 判断的返回值:string,number,boolean,function,object,undfined,symbol
  4. instanceof运算符详解,参考链接:http://www.zuojj.com/archives/393.html
  5. instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另一个要检测对象的原型链上
  6. instanceof后面的操作数必须是一个对象
  7. 继承中判断实例是否属于它的父类
  8. constructor:构造函数;每个对象都有一个constructor属性,指向该对象的构造函数;
  9. prototype:每个对象都有一个prototype属性,指向该对象的原型对象。

数据类型判断方法:

typeof方法,见上面部分代码;

instanceof方法:返回true或false,适用于引用类型,不适合基本数据类型

var x = [1,2,3,"Pelli","Hello World"];
var y = {
year : 2016,
month : 8
}
var z = function(){} var z1 = 'hello';
var z2 = 123;
var z3 = true;
var z4 = null;
var z5 = undefined; console.log(x instanceof Array);//true
console.log(y instanceof Object);//true
console.log(z instanceof Function);//true console.log(z1 instanceof String);//false
console.log(z2 instanceof Number);//false
console.log(z3 instanceof Boolean);//false
console.log(z4 instanceof Object);//false
console.log(z5 instanceof Object);//false

constructor:

var a1 = 'Pelli';
var a2 = 200;
var a3 = true;
var a4 = null;
var a5 = undefined;
var a6 = {}
var a7 = [1,2];
var a8 = function(){} console.log(a1.constructor === String);//true
console.log(a2.constructor === Number);//true
console.log(a3.constructor === Boolean);//true
console.log(a4.constructor === Object);//Uncaught TypeError:Cannot read property 'constructor' of null...
console.log(a5.constructor === Object);//Uncaught TypeError:Cannot read property 'constructor' of undefined...
console.log(a6.constructor === Object);//true
console.log(a7.constructor === Array);//true
console.log(a8.constructor === Function);//true

prototype

var a1 = 'Pelli';
var a2 = 200;
var a3 = true;
var a4 = null;
var a5 = undefined;
var a6 = {}
var a7 = [1,2];
var a8 = function(){}
console.log(Object.prototype.toString.call(a1) === "[object String]");//true
console.log(Object.prototype.toString.call(a2) === "[object Number]");//true
console.log(Object.prototype.toString.call(a3) === "[object Boolean]");//true
console.log(Object.prototype.toString.call(a4) === "[object Null]");//true
console.log(Object.prototype.toString.call(a5) === "[object Undefined]");//true
console.log(Object.prototype.toString.call(a6) === "[object Object]");//true
console.log(Object.prototype.toString.call(a7) === "[object Array]");//true
console.log(Object.prototype.toString.call(a8) === "[object Function]");//true

相关链接:

http://www.cnblogs.com/mindsbook/archive/2009/09/19/javascriptYouMustKnowPrototype.html

http://blog.sina.com.cn/s/blog_51048da70101grz6.html

http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html