日期:2014-05-16  浏览次数:20360 次

javascript判断 对象 类型

/**
?* 返回一个对象的类型
?*/
function getType(x) {
??? //不能!x,因为"",0,false
??? if (null == x)
??????? return "null";

??? //注意:typeof对j引用类型对返回object
??? var t = typeof x;
??? if (t != "object")return t;

??? //有的对象可能重写了toString方法,所以需要调用对象最原始的toString()
??? var s = Object.prototype.toString.call(x),
??????????? s = s.substring(8, s.length - 1);

??? //注意:==“Object”的时候需要判断具体的类型。
??? if (s != "Object")return s;

??? var x_constructor = x.constructor;
??? //x.constructor:即创建x对象的方法
??? if (x_constructor == Object)return s;

??? //一个对象的构造函数的原型对象中如定义有className属性(原本不存在的,这是用户您自己定义的属性),则返回这个属性的值
??? //??? function X(x,y,z){
??? //??????? this.x=x;
??? //??? }
??? //??? X.prototype.className="X";
??? //??? var x=new X(1,2,3);
??? //??? alert(x.constructor.prototype== X.prototype)//return true.
??? var x_cons_proto = x_constructor.prototype;
??? if ("className" in x_cons_proto && typeof x_cons_proto.className == "string") {
??????? return x_cons_proto.className;
??? }

??? //返回构造函数的名称
??? //但是只对于使用函数定义方式声明的函数有效,如:
??? //function ClassA(){this.x="ss"}? var a=new ClassA(); alert(getType(a));
??? if ("name" in x_constructor && "" != x_constructor.name)return x_constructor.name;

??? /*下面都是闭包的情况*/
??? //对与调用Function构造的方式创建的对象,则
??? var c = x_constructor.toString(), c = c.substring(8, c.indexOf("(")).replace(/(^\s*)|(\s*$)/g, "");
??? //对于new方法直接量方式创建的对象,则暂时无能为力
??? if ("" == c) {
??????? return? "The object created by a anonymous function.";
??? }

??? return c;
}

/**
?* 测试
?*/
var printFunc = function() {
??? //--------------------------
??? //--------------------------
??? var k;???????????????????? //<br>
??? document.writeln(getType(k));
??? k = "";
??? document.writeln(getType(k));
??? k = 23;
??? document.writeln(getType(k));
??? k = true;
??? document.writeln(getType(k));
??? k = new Date();
??? document.writeln(getType(k));
??? k = /\s/;
??? document.writeln(getType(k));
??? document.writeln("<br>");
??? document.writeln("<br>");
??? document.writeln("<br>");

??? //--------------------------
??? //--------------------------
??? function Outer(x) {
??????? var x = x;
??????? //... ...
??????? return function() {
??????????? return x;
??????? }
??? }

??? document.writeln(getType(Outer));
??? var o = Outer(98);
??? document.writeln(getType(o));
??? var o = new Outer;
??? document.writeln(getType(o));
??? var o = new Outer(33);
??? document.writeln(getType(o));
??? document.writeln("<br>");
??? document.writeln("<br>");
??? document.writeln("<br>");

??? //--------------------------
??? //--------------------------
??? function Outer2(x) {
??????? this.x = x;
??????? //... ...
??????? this.getX = function() {
??????????? return x;
??????? }
??? }

??? document.writeln(getType(new Outer2(99)));
??? document.writeln("<br>");
??? document.writeln("<br>");
??? document.writeln("<br>");

??? //--------------------------
??? //--------------------------
??? function Outer3(x) {
??????? this.x = x;
??????? //... ...
??????? this.getX = function() {
??????????? return x;
??????? }
??????? return x;
??? }

??? document.writeln(getType(new Outer3(92)));
??? document.writeln("<br>");
??? document.writeln("<br>");
??? document.writeln("<br>");

??? //--------------------------
??? //--------------------------
??? function MyClass(x, y) {
??????? this.x = x;
??????? this.y = y;
??? }

??? //MyClass.prototype.className = "MyClass";