javascript基本数据类型:原始类型 和 引用类型
原始类型(简单数据类型):String,Number,Boolean,Undefined,Null,Symbol(ES6新增)
引用类型(复杂数据类型):Array,Object
1 var a = 'hello world'; 2 var b = 100; 3 var c; 4 var d = null; 5 var e = { 6 name : 'My name is Pelli.' 7 } 8 var f = [100,3,'hello',[4,4,67,'hah'],{age : 34}]; 9 var g = function(){ 10 console.log("I am Pelli."); 11 } 12 13 console.log(typeof a);//'string' 14 console.log(typeof b);//'number' 15 console.log(typeof c);//'undefined' 16 console.log(typeof d);//'object' 17 console.log(typeof e);//'object' 18 console.log(typeof f);//'object' 19 console.log(typeof g);//'function'
关键方法:typeof,instanceof,constructor,prototype
关键点:
- typeof判断的结果,返回值肯定是字符串
- typeof判断的结果的返回值一定在以下六个之中,ES6(也称为ES2015)之后新增一个,一共七个
- typeof 判断的返回值:string,number,boolean,function,object,undfined,symbol
- instanceof运算符详解,参考链接:http://www.zuojj.com/archives/393.html
- instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另一个要检测对象的原型链上
- instanceof后面的操作数必须是一个对象
- 继承中判断实例是否属于它的父类
- constructor:构造函数;每个对象都有一个constructor属性,指向该对象的构造函数;
- 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