instanceof 运算符

80酷酷网    80kuku.com

  运算

 

返回一个 Boolean 值,指出对象是否是特定的一个实例。

result = object instanceof class

参数

result

必选项。任意。

object

必选项。任意对象。

class

必选项。任意已定义的对象类。

说明

如果 objectclass 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 objectnull,则返回 false

示例

下面的例子举例说明了 instanceof 运算符的用法。

function objTest(obj){   var i, t, s = "";   // 创建变量。   t = new Array();   // 创建一个数组。   t["Date"] = Date;   // 填充数组。   t["Object"] = Object;   t["Array"] = Array;      for (i in t)      {         if (obj instanceof t[i])   // 检查 obj 的类。         {            s += "obj is an instance of " + i + "\n";         }         else          {            s += "obj is not an instance of " + i + "\n";         }   }   return(s);   // 返回字符串。}var obj = new Date();response.write(objTest(obj));

要求

请参阅

|



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: