jquery代码阅读之jquery.type
来源:互联网 发布:淘宝营销团队 编辑:程序博客网 时间:2024/04/29 15:06
jquery代码阅读之jquery.type
jquery 源码中有众多地方采用jquer.type 来判断类型 API 参考文档在[这里],(http://api.jquery.com/jQuery.type/)从文档里可以看出改方法对各种类型的返回,在jquery源码中其判断类型的核心方法为Object.prototype.toString 通过toString来判断类型,主要实现方式为:
/*1:定义class2type*/toString = Object.prototype.toString,class2type = {};/*2为class2tpye赋值class2type = { [object Array]: "array", [object Boolean]: "boolean", [object Date]: "date", [object Function]: "function", [object Number]: "number", [object Object]: "object", [object RegExp]: "regexp", [object String]: "string"}*/jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) { class2type[ "[object " + name + "]" ] = name.toLowerCase();});/*type定义 调用toStringObject.prototype.toString.call("hello") "[object String]"Object.prototype.toString.call({hello:"hello"}) "[object Object]"Object.prototype.toString.call([1,2,3]) "[object Array]"*/jQuery.extend({ /*--------其他API-----------*/ type: function( obj ) { return obj == null ? String( obj ) : class2type[ toString.call(obj) ] || "object"; //String(null) 返回"null" }, /* 直接调用jQuery.type的方法*/ isFunction: function( obj ) { return jQuery.type(obj) === "function"; }, /*对于支持isArray的浏览器使用isArray 否则使用jQuery.type*/ isArray: Array.isArray || function( obj ) { return jQuery.type(obj) === "array"; },}
0 0
- jquery代码阅读之jquery.type
- jquery代码阅读jQuery.makeArray()
- jQuery源码阅读之获取jQuery对象
- jquery源码阅读之jquery.extend
- jQuery源码阅读之jQuery.Callbacks ()
- jQuery之代码优化
- jquery源码之属性判断-$.type类型
- jquery源码阅读jQuery.inArray()
- jquery开发之代码风格
- jQuery操作input type=radio的实现代码
- jQuery操作 input type=checkbox的实现代码
- JQuery Selector Type
- Jquery获取Content-type
- jquery event type
- jQuery源码阅读有感
- 阅读jquery--Callbacks
- 阅读jQuery--Deferred
- jquery 插件 阅读
- 花瓶插花
- 三天后深入体会人生体会
- [LeetCode]Combination Sum II
- PyQt5初级教程--PyQt5中部件[8/13]
- [LeetCode]Multiply Strings
- jquery代码阅读之jquery.type
- Tomcat 7 的七大新特性
- txt文件导入mysql
- Swift在for循环中报错'++' is not a binary operator
- 带有输出参数的存储过程
- hdu 题目分类
- 机器学习算法中如何选取超参数:学习速率、正则项系数、minibatch size
- 用C实现打字机模式的一个小程序
- UE4发报机-Event_Assign事件