// Make sure leading/trailing whitespace is removed (IE can’t handle it)
data = jQuery.trim( data );
// Attempt to parse using the native JSON parser first
// 原生JSON API。反序列化是JSON.stringify(object)
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
// … 大致地检查一下字符串合法性
if ( rvalidchars.test( data.replace( rvalidescape, “@” )
.replace( rvalidtokens, “]” )
.replace( rvalidbraces, “”)) ) {
return (new Function( “return ” + data ))();
}
jQuery.error( “Invalid JSON: ” + data );
},
// Cross-browser xml parsing
// (xml & tmp used internally)
// 解析XML 跨浏览器
// parseXML函数也主要是标准API和IE的封装。
// 标准API是DOMParser对象。
// 而IE使用的是Microsoft.XMLDOM的 ActiveXObject对象。
parseXML: function( data , xml , tmp ) {
if ( window.DOMParser ) { // Standard 标准XML解析器
tmp = new DOMParser();
xml = tmp.parseFromString( data , “text/xml” );
} else { // IE IE的XML解析器
xml = new ActiveXObject( “Microsoft.XMLDOM” );
xml.async = “false”;
xml.loadXML( data );
}
tmp = xml.documentElement;
if ( ! tmp || ! tmp.nodeName || tmp.nodeName === “parsererror” ) {
jQuery.error( “Invalid XML: ” + data );
}
return xml;
},
// 无操作函数
noop: function() {},
// Evaluates a script in a global context
// Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
// globalEval函数把一段脚本加载到全局context(window)中。
// IE中可以使用window.execScript。
// 其他浏览器 需要使用eval。
// 因为整个jQuery代码都是一整个匿名函数,所以当前context是jQuery,如果要将上下文设置为window则需使用globalEval。
globalEval: function( data ) {
// data非空
if ( data && rnotwhite.test( data ) ) {
// We use execScript on Internet Explorer
// We use an anonymous function so that context is window
// rather than jQuery in Firefox
( window.execScript || function( data ) {
window[ “eval” ].call( window, data );
} )( data );
}
},
// 判断节点名称是否相同
nodeName: function( elem, name ) {
// 忽略大小写
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
},
// args is for internal usage only
// 遍历对象或数组
each: function( object, callback, args ) {
var name, i = 0,
length = object.length,
isObj = length === undefined || jQuery.isFunction( object );
// 如果有参数args,调用apply,上下文设置为当前遍历到的对象,参数使用args










