這里有個簡單的例子,可以說明 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));