`
zhkac
  • 浏览: 52669 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

jQuery方法扩展:type, toJSON, evalJSON

阅读更多
(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); 

 

分享到:
评论

相关推荐

    jquery.json2.4.js

    Jquery-json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换。可序列化 JavaScript 对象、数值、字符串和数组到 JSON 字符串,同时可转换 JSON 字符串到 JavaScript。 var thing = {plugin: '...

    jquery操作json

    jquery插件 ,可操作JSON,$.toJSON $evalJSON

    jquery.json-2.4.min.js

    toJSON:格式化一个 javascript对象(数组、对象、数字、字符串)为JSON格式。 evalJSON:把格式化后的JSON数据转化回为javascript对象(数组、对象、数字、字符串)

    json-lib-2.3-jdk15.jar包和prototype.js及jquery-1.3.1.js

    json以及与jquery使用时会使用到的包,现提供给大家。 prototype.js是个流行的javascript库,提供了evalJSON()方法

    transport.js

    根本的解决办法是不用transport.js中的json功能,那么就要有一个相同的功能来代替它,这里我选用jquery-json1.3.js。首先要把transport.js中的json功能删除。由于实现json功能的函数有区别,所以要麻烦改掉原ecshop...

    前端开源库-evaljson

    前端开源库-evaljsoneval json,json中的eval嵌入值。用于定义消息资源对象。

    comet 很好的一个实例

    var response = transport.responseText.evalJSON(); WebApp._timestamp_ = response['timestamp']; WebApp.handleResponse(response); WebApp._noerror_ = true; }, onComplete: function...

    Prototype源码浅析 String部分(三)之HTML字符串处理

    unfilterJSON | isJSON | evalJSON | parseJSON 脚本处理 stripScripts | extractScripts | evalScripts现在,String部分转入具体的关联应用,分别对应 HTML字符串,JSON字符串和HTML中的脚本字符串。 ...

Global site tag (gtag.js) - Google Analytics