前面转了一个将jQuery总体框架的文章建议先阅读
var
readyList,
rootjQuery,
core_strundefined = typeof undefined,
location = window.location,
document = window.document,
docElem = document.documentElement,
_jQuery = window.jQuery,
_$ = window.$,
class2type = {},
core_deletedIds = [],
core_version = "1.10.2",
core_concat = core_deletedIds.concat,
core_push = core_deletedIds.push,
core_slice = core_deletedIds.slice,
core_indexOf = core_deletedIds.indexOf,
core_toString = class2type.toString,
core_hasOwn = class2type.hasOwnProperty,
core_trim = core_version.trim,
jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
},
core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,
core_rnotwhite = /\S+/g,
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
rvalidchars = /^[\],:{}\s]*$/,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g,
rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi,
fcamelCase = function( all, letter ) {
return letter.toUpperCase();
},
completed = function( event ) {
if ( document.addEventListener || event.type === "load" || document.readyState === "complete" ) {
detach();
jQuery.ready();
}
},
detach = function() {
if ( document.addEventListener ) {
document.removeEventListener( "DOMContentLoaded", completed, false );
window.removeEventListener( "load", completed, false );
} else {
document.detachEvent( "onreadystatechange", completed );
window.detachEvent( "onload", completed );
}
};
以上定义了一些函数变量和方法(这样解释不知道对不对)
并且jQuery是在一个匿名函数中,用了var就避免了全局变量的使用,这样的好处就是不会和其他库或者插件冲突
但是jQuery还是有一个全局变量就是$
其中
jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
},
上面这段代码作用就是构建JQuery对象,实际上我们用$()返回的就是jQuery.fn.init对象的实例,
根据js语法我们就可以看出来,这个对象的prototype指向了jQuery的prototype。这样实现了链式编程操作。
function(window, undefined),undefined参数传进来的好处是避免了undefined变量被污染。