Skip to content

jQuery核心解读 #51

Open
Open

Description

@Yangxiaqing

知识点 一:

window.jQuery = window.$ = jQuery——(jQuery就是闭包中的私有的函数)

	var jQuery = function (selector, context) {
		return new jQuery.fn.inint(selector, context);
               //我们可以把这个理解为返回的是一个jQuery类的实例
	};

	//$("", document)
	//$(this)
	$(function (){
		
	})
	jQuery()

selector——支持[string]选择器的类型、[object]JS原生的对象(把原生的对象转化为jQuery对象)、[function]回调函数(等价于我们的$(document).ready...)
context——不传默认结果是document,它传递的是一个原生的JS对象能够上下文,如果是一个jQuery对象,它默认会把其重构为选择器

知识点 二:

jQuery.fn = jQuery.prototype

	jQuery.fn = jQuery.prototype = {
		constructor: jQuery
	};

jQuery选择器:创建jQuery这个类的一个实例(jQuery对象)
在jQuery这个类的原型上定义了很多的属性和方法,而通过选择器获取的每一个实例都可以调用这些方法:属性、CSS、文档操作、筛选、动画、事件。。。

$("*").css();

知识点三:

jQuery.extend = jQuery.fn.extend = function() {}

jQuery.extend = jQuery.fn.extend = function() {
	//jQuery.extend:把它当做一个普通的对象,在对象中增加了extend方法
	//jQuery.fn.extend:在它的原型上也增加了一个extend方法
};

把jQuery当做一个普通的对象,在它自己的属性上增加了一些常用的方法:Ajax、工具。。。

$.ajax();

总结:

extend——扩展,向jQuery这个类库中增加一些其他常用的方法

  • 向JQuery属性名上扩展(把它当做对象)——完善类库,给类库增加核心的方法
jQuery.extend({
	aa: function () {
		console.log("aa");
	}
});
$.aa();
  • 向JQuery原型上扩展(把它当做一个类)——编写一些基于jQuery插件
jQuery.fn.extend({
	bb: function () {
		console.log("bb");
	}
});
$().bb;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions