/**
?* 返回一个对象的类型
?*/
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"; //自定义一个属性,表示类型,则会提高执行效率
??? //MyClass.prototype.constructor = MyClass;
??? MyClass.prototype.getSum = function() {
??????? return this.x + this.y;
??? };
??? var mc = new MyClass(2, 5);
??? document.writeln(mc.getSum());
??? document.writeln(getType(mc));
??? document.writeln("<br>");
??? document.writeln("<br>");
??? document.writeln("<br>");
??? //--------------------------
??? //--------------------------
??? var MyClass3 = function(x, y) {
??????? this.x = x;
??????? this.y = y;
??? }
??? //MyClass.prototype.className = "MyClass";
??? //MyClass.prototype.constructor = MyClass;
??? MyClass3.prototype.getSum = function() {
??????? return this.x + this.y;
??? };
??? mc = new MyClass3(1, 5);
??? document.writeln(mc.getSum());
??? document.writeln(getType(mc));
??? document.writeln("<br>");
??? document.writeln("<br>");
??? document.writeln("<br>");
??? //--------------------------
??? //--------------------------
??? var MyClass2 = Function("x", "y", "this.x = x;this.y = y;");
??? MyClass2.prototype.getSum = function() {
??????? return this.x + this.y;
??? };
??? mc = new MyClass2(3, 5);
??? document.writeln(mc.getSum());
??? document.writeln(getType(mc));
}
printFunc();
//document.writeln("<br>");
//document.writeln(printFunc);
详细解决方案
javascript判断 对象 门类
热度:409 发布时间:2012-10-12 10:17:04.0
相关解决方案
- javascript ie6兼容的有关问题
- javascript window open在ie中设立不起作用,求解决
- javascript 字符串拼接效率有关问题
- JavaScript 自动生成图片并合并有关问题
- 不走"<script type='text/javascript'>"标签咋回事
- <script type="javascript/text">的有关问题
- 用servlet+jsp+javascript+jdbc做个简单的办公自动化系统流程,该如何解决
- 怎么打开 javascript:SetData(2010,5,10)
- javaScript = == ===区别,该怎么解决
- javascript 怎么验证name=xx.xx的radio表单
- form action 和 javascript 的提交問題解决方法
- javascript,该怎么处理
- javascript,该如何处理
- javascript 选中文字 但是保存样式 标签
- 新人求问,J2EE方向,html,css,javascript,vml要学到什么程度?解决思路
- javascript 请求servlet兑现将函数中定义的变量作为参数
- javascript 不懂,该如何处理
- javascript 不懂解决方法
- JavaScript 大局函数求实例,高分求
- javaScript 里面 如何知道Object 对象的长度
- javascript 函数调用有什么有关问题,请
- javascript 中文本框中数字如何比较
- javascript IE通过,火狐,google浏览器不过解决思路
- javascript rsa加密/java使用Cipher.getInstance("RSA/ECB/PKCS1Padding")解密,该如何处理
- IE javascript start()函数解决方案
- 关于RTMP 播放器(DELPHI C# FLASH JAVASCRIPT)解决思路
- Chrome Javascript Click 事件,该如何解决
- javascript 实出_blank跳转到新标签页有关问题
- 分享上Google Maps Javascript API v3
- javascript 绑定服务器控件 事件,该如何解决