5种基础数据范例:Undefined、Null、Boolean、Number和String。
1中庞杂数据范例:Object
运用typeof检测
typeof操作符返回一个字符串,示意未经求值的操作数的范例。
typeof能够的返回值:
“undefined”——假如这个值未定义;
“boolean”——假如这个值是布尔值;
“string”——假如这个值是字符串;
“number”——假如这个值是数值;
“object”——假如这个值是对象或 null;
“function”——假如这个值是函数。
注意事项:
typeof null 返回 object。
typeof 是一个操作符而不是函数,圆括号只管能够运用,但不是必须的。
//typeof str 或许 typeof(str) 均能够
运用instanceof检测
用来检测援用范例:晓得一个值是什么范例的对象。返回true/false。
一切援用范例的值都是 Object 的实例。
假如运用 instanceof 操作符检测基础范例的值,则该操作符始终会返回 false,由于基础范例不是对象。然则运用new关键字组织基础数据的包装对象的实例时instanceof操作符也会返回true。(instanceof只适用于组织函数建立返回的庞杂对象和实例。)
用instanceof检测undefined和null是否是Object实例时,返回false。
function Person(){}
function Student(){}
Student.prototype = new Person();
var John = new Student();
console.log(John instanceof Student); // true
console.log(John instancdof Person); // true
console.log(John instancdof Object); // true
var a;
用instanceof检测undefined和null是否是Object实例时,返回false。
typeof a; //"undefined"
a instanceof Object; //false
var b = null;
typeof b; //"object"
b instanceof Object; //false