js中typeof与instanceof用法

1 typeof

typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果:
number,boolean,string,function(函数),object(NULL,数组,对象),undefined。

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

2 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

猜你喜欢

转载自blog.csdn.net/qq_27870421/article/details/89462592