【js】typeof与instanceof

typeof 运算符

返回一个用来表示表达式的数据类型的字符串。

typeof[()expression[]] ;

expression 参数是需要查找类型信息的任意表达式。

说明

typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."

typeof 语法中的圆括号是可选项。

例如

typeof的运算数未定义,返回的就是 "undefined".

运算数为数字 typeof(x) = "number"

字符串 typeof(x) = "string"

布尔值 typeof(x) = "boolean"

对象,数组和null typeof(x) = "object"

函数 typeof(x) = "function"

如: 
alert(typeof (123));//typeof(123)返回"number" 
alert(typeof ("123"));//typeof("123")返回"string"

 instanceof运算符

instanceof用于判断一个变量是否某个对象的实例,

var a=new Array();

alert(a instanceof Array);//会返回true,

同时alert(a instanceof Object)也会返回true;这是因为Array是object的子类。

再如:

function test(){};

var a=new test();

alert(a instanceof test);//会返回true。

原文地址:https://www.cnblogs.com/ningvsban/p/3426512.html