首页 / JQUERY / jQuery 各类判断函数汇总
jQuery 各类判断函数汇总
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jQuery 各类判断函数汇总,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2146字,纯文字阅读大概需要4分钟。
内容图文
1、类型type
type: function( obj ) { if ( obj == null ) { return String( obj ); } // Support: Safari <= 5.1 (functionish RegExp)// 利用事先存好的 hash 表 class2type 作精准判断returntypeof obj === "object" || typeof obj === "function" ? class2type[ core_toString.call(obj) ] || "object" : typeof obj; },
首先其修正了 typeof null 为object的缺陷。其次利用事先存好的 hash 表 class2type 作精准判断。
其中core_toString=obj.toString;obj是一个对象
var obj={};arr=[]; console.log(obj.toString.call(arr));//[object Array] console.log(obj.toString.call(obj));//[object Object]
2、空对象isEmptyObject
// 检查对象是否为空(不包含任何属性) isEmptyObject: function( obj ) { var name; //对于空对象是不会执行for循环语句的for ( name in obj ) { returnfalse; } returntrue; },
3、数字isNumeric
// 确定它的参数是否是一个数字 // isFinite判断数组的元素是否是有界的 isNumeric: function( obj ) { return !isNaN( parseFloat(obj) ) && isFinite( obj ); },
4、函数isFunction
isFunction: function( obj ) { return jQuery.type(obj) === "function"; },
主要是利用前面的type.
5、window
// 判断传入对象是否为 window 对象 isWindow: function( obj ) { return obj != null && obj === obj.window; },
6、isArray
// 判断传入对象是否为数组 isArray: Array.isArray,
利用数组自带的isArray来判断
var arr=[]; console.log(Array.isArray(arr));//true
7、isPlainObject
// 测试对象是否是纯粹的对象 // 通过 "{}" 或者 "new Object" 创建的 isPlainObject: function( obj ) { // Not plain objects:// - Any object or value whose internal [[Class]] property is not "[object Object]"// - DOM nodes// - window// Make sure that DOM nodes and window objects don‘t pass through, as wellif ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { returnfalse; } // Support: Firefox <20// The try/catch suppresses exceptions thrown when attempting to access// the "constructor" property of certain host objects, ie. |window.location|// https://bugzilla.mozilla.org/show_bug.cgi?id=814622try { if ( obj.constructor && !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) { returnfalse; } } catch ( e ) { returnfalse; } // If the function hasn‘t returned already, we‘re confident that// |obj| is a plain object, created by {} or constructed with new Objectreturntrue; },
原文:http://www.cnblogs.com/huansky/p/5612035.html
内容总结
以上是互联网集市为您收集整理的jQuery 各类判断函数汇总全部内容,希望文章能够帮你解决jQuery 各类判断函数汇总所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。