原文:jQuery方法扩展:type, toJSON, evalJSON. http://zhkac.iteye.com/blog/499330 .2013-05-19

(function($) {
// the code of this function is from
// http://lucassmith.name/pub/typeof.html
$.type = function(o) {
var _toS = Object.prototype.toString;
var _types = {
'undefined': 'undefined',
'number': 'number',
'boolean': 'boolean',
'string': 'string',
'[object Function]': 'function',
'[object RegExp]': 'regexp',
'[object Array]': 'array',
'[object Date]': 'date',
'[object Error]': 'error'
};
return _types[typeof o] || _types[_toS.call(o)] || (o ? 'object' : 'null');
};
// the code of these two functions is from mootools
// http://mootools.net
var $specialChars = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' };
var $replaceChars = function(chr) {
return $specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16);
};
$.toJSON = function(o) {
var s = [];
switch ($.type(o)) {
case 'undefined':
return 'undefined';
break;
case 'null':
return 'null';
break;
case 'number':
case 'boolean':
case 'date':
case 'function':
return o.toString();
break;
case 'string':
return '"' + o.replace(/[\x00-\x1f\\"]/g, $replaceChars) + '"';
break;
case 'array':
for (var i = 0, l = o.length; i < l; i++) {
s.push($.toJSON(o[i]));
}
return '[' + s.join(',') + ']';
break;
case 'error':
case 'object':
for (var p in o) {
s.push(p + ':' + $.toJSON(o[p]));
}
return '{' + s.join(',') + '}';
break;
default:
return '';
break;
}
};
$.evalJSON = function(s) {
if ($.type(s) != 'string' || !s.length) return null;
return eval('(' + s + ')');
};
})(jQuery);

最新文章

  1. WinForm常用属性
  2. CODEVS3037 线段覆盖 5[序列DP 二分]
  3. Spring整合Redis
  4. qt 标准对话框
  5. supervisor拉起daemon进程(falcon-agent)测试
  6. Atitit 项目的主体设计与结构文档 v3
  7. 解析Json需要设置Mime
  8. Centos7网络监控
  9. Linux 执行ThinkPHP 文件的计划任务
  10. DevExpress控件学习总结 z
  11. StringHelpers
  12. Python4Delphi也是与VCL密切相关,所以才能相互调用,绝对有研究价值!
  13. 零成本建立的.NET小组开发平台
  14. Effective Java 之 --- 用私有构造器或者枚举类型强化Singleton属性
  15. 洛谷【P2257】YY的GCD
  16. JVM(二)之GC(转)
  17. select 查询
  18. 使用Xcode打包上传APP
  19. spring---aop(5)---Spring AOP的配置的背后的配置
  20. (转)获取android源码时repo的错误

热门文章

  1. Java 中 方法名或类名 变更 同时 更新 所有引用的 类名或方法名 的解决方案
  2. Maven核心概念之仓库,生命周期与插件
  3. CLS(公共语言规范)的CLSCompliant(跨语言调用)
  4. 表结构设计器(EZDML)1.98版公布
  5. HTTP 头缓存Last-Modified,ETag,Expires
  6. webservice接口示例(spring+xfire+webservice)
  7. 娓娓道来c指针 (4)解析c的声明语句
  8. 解析theme()
  9. C# 判断是否是节假日
  10. apache绑定多个域名