jQuery框架源码解读

1.jQuery 1.9.1

 1 parseJSON: function( data ) {
 2         // Attempt to parse using the native JSON parser first
 3         if ( window.JSON && window.JSON.parse ) {
 4             return window.JSON.parse( data );
 5         }
 6 
 7         if ( data === null ) {
 8             return data;
 9         }
10 
11         if ( typeof data === "string" ) {
12 
13             // Make sure leading/trailing whitespace is removed (IE can't handle it)
14             data = jQuery.trim( data );
15 
16             if ( data ) {
17                 // Make sure the incoming data is actual JSON
18                 // Logic borrowed from http://json.org/json2.js
19                 if ( rvalidchars.test( data.replace( rvalidescape, "@" )
20                     .replace( rvalidtokens, "]" )
21                     .replace( rvalidbraces, "")) ) {
22 
23                     return ( new Function( "return " + data ) )();
24                 }
25             }
26         }
27 
28         jQuery.error( "Invalid JSON: " + data );
29     },

 

posted @ 2014-03-13 22:53  未来动力  阅读(216)  评论(0编辑  收藏  举报