array2json
2013-09-27 10:37
483 查看
原文: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);
相关文章推荐
- 动态加载js
- 几个求值js
- 有用的JS函数
- 你真的搞懂Javascript了吗?(5道测试题)
- JavaScript中json对象和string对象之间的转化
- Extjs取得某个控件并赋值
- Pace.js – 超赞的页面加载进度自动指示和 Ajax 导航效果
- JS 存放目录说明
- 基于Liferay的平台下,portlet在各个模式下分别加载以<footer-portlet-javascript>定义的js文件的不可行性 推荐
- 打开,另存为,属性,打印 JS代码
- JS Bin Tips and Bits • About
- js小知识
- 用javascript做一个计算器??
- Sass详解之编译工具koala
- js时间显示样式源代码
- jsp页面处理参数文字乱码
- javascriptmvc—can.Construct
- javascript对象属性排序
- PHP JS Ip地址及域名格式检测代码
- javascript中call和apply方法浅谈