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。



,明天是世上增值最快的一块土地,因它充满了希望

js typeof instanceof

相关文章:

你感兴趣的文章:

标签云: