《jQuery 技术内幕》读书笔记(2)—— 第2章 构造函数 jQuery() 和 jQuery.fn.init()

本文是《jQuery 技术内幕》第二章读书笔记,详细探讨了jQuery构造函数和jQuery.fn.init方法,包括构造函数的基本使用、内部结构以及init方法的参数selector和context的11种不同处理分支,为理解jQuery核心工作原理提供深入理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#《jQuery 技术内幕》读书笔记(2)—— 第2章 构造函数 jQuery() 和 jQuery.fn.init ()

注意:代码部分来自 jquery 1.12.4

1.构造函数 jQuery()

在这里插入图片描述

2.总体结构

// 当 jQuery 初始化时,里面的代码被执行
(function( global, factory ) {

	if ( typeof module === "object" && typeof module.exports === "object" ) {
		/*
		 For CommonJS and CommonJS-like environments where a proper `window`
		 is present, execute the factory and get jQuery.
		 For environments that do not have a `window` with a `document`
		 (such as Node.js), expose a factory as module.exports.
		 This accentuates the need for the creation of a real `window`.
		 e.g. var jQuery = require("jquery")(window);
		 See ticket #14549 for more info.
		 */
		module.exports = global.document ?
			factory( global, true ) :
			function( w ) {
				if ( !w.document ) {
					throw new Error( "jQuery requires a window with a document" );
				}
				return factory( w );
			};
	} else {
		factory( global );
	}

// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
	// 一堆局部变量申明
	/* 
		1.Define a local copy of jQuery
	*/
	var jQuery = function( selector, context ) {
		// The jQuery object is actually just the init constructor 'enhanced'
		// Need init if jQuery is called (just allow error to be thrown if not included)
		// 为了使用的时候可以省去前面的 new 运算符
		return new jQuery.fn.init( selector, context );
	}
	/* 
		2.覆盖 jQuery() 的原型对象
		jQuery.fn 是 jQuery.prototype 的简写,可以省去几个字
	*/
	jQuery.fn = jQuery.prototype = {
		constructor: jQuery,
		// 一堆原型属性和方法
	}
	/* 
		3.负责解析 selector 和 context 的类型并执行相应的查找
	*/
	var init = jQuery.fn.init = function( selector, context, root ) {}
	/* 
		4.用 jQuery 构造函数的原型对象 jQuery.fn 覆盖 jQuery.fn.init() 的原型对象
	*/
	init.prototype = jQuery.fn;
	/* 
		5.用于合并两个或多个对象的属性到第一个对象
	*/
	jQuery.extend = jQuery.fn.extend = function() {}
	/* 
		6.定义一堆静态属性和方法
	*/
	jQuery.extend( {} )
	/* 
		7.把 jQuery 变量暴露给全局作用域 window, 并定义别名 $
	*/
	if ( !noGlobal ) {
		window.jQuery = window.$ = jQuery;
	}
	return jQuery;
}));

3.jQuery.fn.init ( selector, context, root )

  • 参数 selector 和 context 共有 11 个分支
selectorcontext 例子构造函数
1可以转换为 false —— $(),$(""),$(undefined),$(null),$(false)
2字符串#idundefined$("#id")jQuery( selector[, context])
3选择器表达式undefined$("div p")
4选择器表达式jQuery 对象$("div p", $("#id"))
5选择器表达式DOM 元素$("span", this)
6单独标签—— $( "<div>")
$( "<div>", { "class": "test" })
jQuery( html[, ownerDocument] )
jQuery( html, props)
7复杂HTML——$( "<div id='info'>这是信息</div>" )
8DOM 元素——$( document )jQuery( element )
jQuery( elementArray ),
9函数——$( function(){ ... } )jQuery( callback )
10jQuery 对象——$( $("div p") )jQuery( jQuery object )
11其他任意对象——$( { abc: 123 } )
$( [ 1,2,3 ] )
jQuery( object )
  • 11 个分支,判断条件和执行过程
jQuery.fn.init( selector, context, root )
11个分支
条件处理方式
!selector(""), $(null), $(undefined), $(false)return this;
字符串的6个分支#id document.getElementById
this.context = document;
this.selector = selector;
return this;
$(selector)return ( context || root ).find( selector );
$(selector, $(selector))
$(selector, context)return this.constructor( context ).find( selector );
单独标签[ context.createElement ]
return jQuery.merge( this, [...] );
复杂 HTML 代码jQuery.buildFragment
return jQuery.merge( this, [...] );
functionreturn typeof root.ready !== "undefined" ?
root.ready( selector ) :
// Execute immediately if ready is not present
selector( jQuery );
$( $(...) )this.selector = selector.selector;
this.context = selector.context;
其他任意值return jQuery.makeArray( selector, this );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值