JavaScript中,与判断变量的类型相关的,有2个运算符:typeof、instanceof,和一个属性constructor。
通过一个测试的例子来说明。使用Boolean、String、Array和一个自己先创建的类,代码如下所示:
<script language="Javascript"> var myBool = new Boolean(); document.write("Boolean.constructor = " + Boolean.constructor + ".<BR>"); document.write("Boolean.constructor() = " + Boolean.constructor() + ".<BR>"); document.write("myBool.constructor = " + myBool.constructor + ".<BR>"); document.write("myBool.constructor() = " + myBool.constructor() + ".<BR>"); if(typeof(myBool) == Boolean){ document.write("typeof(myBool) = <B>Boolean</B>.<BR>"); } else{ document.write("typeof(myBool) = <B>" + typeof(myBool) +"</B><BR>"); } if(myBool instanceof Boolean){ document.write("myBool is a instance of <B>Boolean</B>.<BR><BR>"); } else if(myBool instanceof [Object]){ document.write("myBool is a instance of <B>Object</B>.<BR><BR>"); } var myStr = new String("Hello,Shirdrn!!!"); document.write("String.constructor = " + String.constructor + ".<BR>"); document.write("String.constructor() = " + String.constructor() + ".<BR>"); document.write("String.constructor = " + String.constructor + ".<BR>"); document.write("String.constructor() = " + String.constructor() + ".<BR>"); if(myStr.constructor() == String){ var sc = myStr.constructor(); if(sc == ""){ document.write("myStr.constructor() = \"\".<BR>"); } } if(typeof(myStr) == String){ document.write("typeof(myStr) = String.<BR>"); } else{ document.write("typeof(myStr) = <B>" + typeof(myStr) +"</B><BR>"); } if(myStr instanceof String){ document.write("myStr is a instance of <B>String</B>.<BR><BR>"); } else if(myArray instanceof Object){ document.write("myStr is a instance of <B>Object</B>.<BR><BR>"); } var myArray = ["姓名:Shirdrn","年龄:26","Email:shirdrn@hotmail.com"]; document.write("Array.constructor = " + Array.constructor + ".<BR>"); document.write("Array.constructor() = " + Array.constructor() + ".<BR>"); document.write("myArray.constructor = " + myArray.constructor + ".<BR>"); document.write("myArray.constructor() = " + myArray.constructor() + ".<BR>"); if(typeof(myArray) == Array){ document.write("typeof(myArray) = <B>Array</B>.<BR>"); } else{ document.write("typeof(myArray) = <B>" + typeof(myArray) +"</B><BR>"); } if(myArray instanceof Array){ document.write("myArray is a instance of <B>Array</B>.<BR><BR>"); } else if(myArray instanceof Object){ document.write("myArray is a instance of <B>Object</B>.<BR><BR>"); } function Building(){ var cost = 0; this.setCost = function(c){ cost = c; }; this.getCost = function(){ return cost; }; }; Building.prototype.located = ""; Building.prototype.projectTeam = "山东第一建筑团队"; Building.prototype.style = "European Style"; Building.prototype.planning = function(){ var pre = "2月1日:建筑工程全面启动。<BR>"; pre += "2月1日~11月30日:工程建筑时期。<BR>"; pre += "11月30日~12月31日:工程检测时期。<BR>"; pre += "1月1日:完美竣工。<BR>"; return pre; }; var myBuilding = new Building(); document.write("Building.constructor = " + Building.constructor + ".<BR>"); document.write("Building.constructor() = " + Building.constructor() + ".<BR>"); document.write("myBuilding.constructor = " + myBuilding.constructor + ".<BR>"); document.write("myBuilding.constructor() = " + myBuilding.constructor() + ".<BR>"); if(typeof(myBuilding) == Building){ document.write("typeof(myBuilding) = <B>Building.<BR><BR>"); } else{ document.write("typeof(myBuilding) = <B>" + typeof(myBuilding) +"</B><BR>"); } if(myBuilding instanceof Building){ document.write("myBuilding is a instance of <B>Building</B>.<BR><BR>"); } else if(myBuilding instanceof Object){ document.write("myBuilding is a instance of <B>Object</B>.<BR><BR>"); } </script> |
运行结果如下所示:
Boolean.constructor = function Function() { [native code] } . Boolean.constructor() = function anonymous() { }. myBool.constructor = function Boolean() { [native code] } . myBool.constructor() = false. typeof(myBool) = object myBool is a instance of Boolean.
String.constructor = function Function() { [native code] } . String.constructor() = function anonymous() { }. myStr.constructor = function String() { [native code] } . myStr.constructor() = . typeof(myStr) = object myStr is a instance of String.
Array.constructor = function Function() { [native code] } . Array.constructor() = function anonymous() { }. myArray.constructor = function Array() { [native code] } . myArray.constructor() = . typeof(myArray) = object myArray is a instance of Array.
Building.constructor = function Function() { [native code] } . Building.constructor() = function anonymous() { }. myBuilding.constructor = function Building(){ var cost = 0; this.setCost = function(c){ cost = c; } this.getCost = function(){ return cost; } }. myBuilding.constructor() = undefined. typeof(myBuilding) = object myBuilding is a instance of Building. |
通过程序及其运行结果可以得到如下结论:
JavaScript的类的constructor属性通过测试都是如下形式的:
function Function() { [native code] } . |
而一个类的constructor方法是如下形式的:
function anonymous() { }. |
一个类的对象实例的constructor属性,对于JavaScript内部类是如下这样:
function Array() { [native code] } . |
但是对于我们自己创建的JavaScript类,它会得到我们在类的构造函数中设置的内容,如Building类的一个对象实例myBuilding的constructor属性如下所示:
function Building(){ var cost = 0; this.setCost = function(c){ cost = c; }; this.getCost = function(){ return cost; }; }. |
对于类的对象实例的constructor方法(即constructor()),JavaScript内部类的对象实例都有自己的默认值,比如Boolean的对象实例的是的constructor()值为false,而String的对象实例的是的constructor()值为"",Array的对象实例的是的constructor()值为"";但是对于我们自己创建的类就是undefined的。