`

jQuery.typeof and jQuery.isEmpty Utilities

阅读更多

One of the best aspects of jQuery is its light weight. I have really taken advantage of some of its built in Utilities such as jQuery.extend() and jQuery.map(). I am definitely against bloated software, but I find myself writing these functions time and again. They are pretty utilitarian and not too huge. Just a suggestion (code borrowed from Crockford's Remedial JavaScript)

 

1. A better typeof function that actually returns 'array' for an array instead of 'object':

function typeOf(value) { 

var s = typeof value; if (s === 'object') { 

if (value) { 

if (typeof value.length === 'number' && 

!(value.propertyIsEnumerable('length')) && typeof value.splice === 'function') { 

s = 'array'; 

} 

} else { 

s = 'null'; 

} 

} return s; 

} 

 

2. A simple isEmpty function that returns a boolean:

function isEmpty(o) { 

var i, v; if (typeOf(o) === 'object') { 

for (i in o) { 

v = o[i]; if (v !== undefined && typeOf(v) !== 'function') { 

return false; 

} 

} 

} return true; 

} 

 

分享到:
评论

相关推荐

    最新jquery.min.js

    •Deferred:当typeof( target)=='object'时,Deferred.promise( target)才能正常工作 •Event:使用委托事件和伪类时的性能衰退问题 •Misc:修复了一些兼容问题,并进行了优化 •Offset:在BlackBerry5和iOS3上...

    jquery.params.js

    jQuery.query = new function () { var is = function (o, t) { return o != undefined && o !== null && (!!t ? o.constructor == t : true); }; var parse = function (path) { var m, rx = /\[([^[]*)\]/g, ...

    jquery.form.js

    if(typeof(_rollback) == "string"){ options.target = _rollback; } options.beforeSubmit = function(formData, jqForm, options){ $('#'+_formId + " :input").blur(); if($('#'+_formId).errors....

    5.typeof和数据类型转换.md

    5.typeof和数据类型转换.md

    js表单源码 jquery.form.rar

    js表单源码 jquery.form.rar 简单实用的工具 花了很多搞到的。珍惜使用 (function (factory) { "use strict"; if (typeof define === 'function' && define.amd) { // using AMD; register as anon module ...

    图形jquery.gvChart-1.0.1.min.js

    // Open a new window and print selected content var w = window.open(); w.[removed](content); w.document.close(); return printFrame(w); } function isNode(o) { /* ...

    jQuery基础

    if ( typeof selector === "string" ) { // Are we dealing with HTML string or an ID? var match = quickExpr.exec( selector ); // Verify a match, and that no context was specified for #id if...

    jQuery.cookie.js实现记录最近浏览过的商品功能示例

    本文实例讲述了jQuery.cookie.js实现记录最近浏览过的商品功能。分享给大家供大家参考,...= 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; op

    jquery-1.8.2.min.js

    Deferred:当typeof( target)=='object'时,Deferred.promise( target)才能正常工作 Event:使用委托事件和伪类时的性能衰退问题 Misc:修复了一些兼容问题,并进行了优化 Offset:在BlackBerry5和iOS3上使用会抛...

    100套Java源码-nuevo:/*!jQueryv1.9.1|(c)2005、2012jQueryFoundation,Inc.|jque

    typeof t,o = e.document,a = e.location,s = e。 jQuery,u = e。$,l = {},c = [],p =“ 1.9.1”,f = c.concat,d = c.push,h = c.slice,g = c.indexOf, m = l.toString,y = l.hasOwnProperty,v = p....

    检测jQuery.js是否已加载的判断代码

    当然,该方法不局限于jQuery的...if (typeof jQuery == 'undefined') { // jQuery 未加载 } else { // jQuery 已加载 } 备注: 以上我们通过检测jQuery函数是否已定义,这是一个比较安全可靠的方法,因为当你加载jQuer

    jquery选择器原理介绍($()使用方法)

    每次申明一个jQuery对象的时候,返回的是jQuery.prototype.init对象,很多人就会不明白,init明明是jQuery.fn的方法啊,实际上这里不是方法,而是init的构造函数,因为js的prototype对象可以实现继承,加上js的对象...

    jQuery使用动态渲染表单功能完成ajax文件下载

    代码如下:// Ajax 文件下载 jQuery.download = function(url, data, method){ // 获取url和data if( url && data ){ // data 是 string 或者 array/object data = typeof data == ‘string’ ? data : jQuery.param...

    typeof:在 Javascript 中检查类型的更好方法

    typeof ( "array" , function ( ) { //this matches an array lets do something with it. } ) foo . typeof ( "string" , function ( ) { //this doesn't match so it won't do anything but if it does detect a ...

    jquery 最新版框架下载(1.32-1.8.2)

    •Deferred:当typeof( target)=='object'时,Deferred.promise( target)才能正常工作 •Event:使用委托事件和伪类时的性能衰退问题 •Misc:修复了一些兼容问题,并进行了优化 •Offset:在BlackBerry5和iOS3上...

    jquery 最新版框架下载(1.32-1.8.3)

    jQuery是JavaScript语言的一个新的资源库(框架) 为方便大家使用将jquery1.7 中文手册打包到里面了 jQuery能快速,简洁的使用HTML documents, handle events, perform animations,并且能把Ajax交互应用到网页,jQuery...

    jquery实现页面关键词高亮显示的方法

    本文实例讲述了jquery实现页面关键词高亮显示的方法。分享给大家供大家参考。... if(typeof(search) == ‘undefined’) return;  var configs = jQuery.extend({  insensitive: 1, //是否匹配大小

    jquery1.9.1中文版

    jquery中文汉化版 (function( window, undefined ) { //不要做这个因为各自的应用程序包括ASP.NET查找 // the stack via arguments.caller.callee and Firefox dies if //你尝试查找通过“精确使用”呼叫链接(#...

    jQuery链式调用与show知识浅析

    上篇文章给大家介绍了jQuery的框架,有关jquery的基础知识可以参考下。 jQuery使用许久了,但是有一些API的实现实在想不通。下面将使用简化的代码来介绍,主要关注jQuery的实现思想。 相较于上一篇,代码更新了:21...

    jquery提交form表单时禁止重复提交的方法

    代码如下:$(document).ready(function() { $(‘form’).submit(function() { if(typeof jQuery.data(this, “disabledOnSubmit”) == ‘undefined’) { jQuery.data(this, “disabledOnSubmit”, { submited: ...

Global site tag (gtag.js) - Google Analytics