【笔记】 《js权威指南》- 第9章 类和模块 - 9.5 类和类型
2016-04-03 19:10
441 查看
如何识别一个类:
1.instanceof运算符:
判断对象是否是某个类的实例
2.constructor属性:
类的构造函数
3.构造函数的名称:
缺点:不支持没有constructor属性的对象, 不是所有构造函数有名字
1.instanceof运算符:
判断对象是否是某个类的实例
var a = new AnyClass(); //true a instanceof AnyClass; AnClass.prototype.isPrototypeOf(a);
2.constructor属性:
类的构造函数
//true a.constructor === AnyClass
3.构造函数的名称:
缺点:不支持没有constructor属性的对象, 不是所有构造函数有名字
function type(o) { //type, class, name var t, c, n; if (o === null) return "null"; //NaN和自身不相等 if (o !== o) return "nan"; //可识别出原始值得类型和函数 if ((t = typeof o) !== "object") return t; if ((c = classof(o)) !== "Object") retrn c; if (o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n; return "Object"; } function classof(o) { return Object.prototype.tpString.call(o).slice(8, -1); }; Function.prototype.getName = function () { if ("name" in this) return this.name; return this.name = this.toString().match(/function\s*([^(]*)\(/)[1]; };
相关文章推荐
- php中instanceof 与 is_a()区别分析
- JavaScript精炼之构造函数 Constructor及Constructor属性详解
- Array, Array Constructor, for in loop, typeof, instanceOf
- instanceof和typeof运算符的区别详解
- javascript instanceof,typeof的区别
- javascript之typeof、instanceof操作符使用探讨
- JavaScript constructor和instanceof,JSOO中的一对欢喜冤家
- 不用构造函数(Constructor)new关键字也能实现JavaScript的面向对象
- JavaScript中几个重要的属性(this、constructor、prototype)介绍
- 理解Javascript_11_constructor实现原理
- PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
- 详解PHP中instanceof关键字及instanceof关键字有什么作用
- JavaScript中constructor()方法的使用简介
- JS中typeof与instanceof之间的区别总结
- 解析JavaScript中instanceof对于不同的构造器或许都返回true
- javascript new后的constructor属性
- 深入分析js中的constructor和prototype
- 谈谈我对JavaScript中typeof和instanceof的深入理解
- Android获取当前运行的类名或者方法
- instanceof关键字