js typeof instanceof

时间:2023-03-10 07:46:53
js typeof  instanceof

一般都是用typeof推断变量存在

例如if(typeof a!="undefined"){}。不是要去使用if(a)因为假定a不存在(未申报)将是错误的。

由于typeof经验null,排列,返回object类型。所以当我们要推断一个对象是否是数组时

或者推断某个变量是否是某个对象的实例则要选择使用还有一个关键语法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。



版权声明:本文博主原创文章。博客,未经同意不得转载。