jQuery源码分析(一)

- 日理万妓 2022-12-29 14:15 255阅读 0赞

jQuery源码分析(一)

我们知道在jQuery中在使用选择器或者给元素绑定事件的时候都是通过$来操作的。那么基于JavaScript面向对象的思想,我们可以把jQuery看做一个函数或者对象,它里边存储了大量的方法,是一个类库。
$代表的就是jQuery
$.ajax() ————- jQuery.ajax() 当做对象来看就是jQuery调用它静态私有的属性和方法,同样我们可以再控制台输出一下dir(jQuery)看到jQuery对象中存储属性和方法:
dir(jQuery)
jQuery.prototype 原型上存储的就是供jQuery实例对象调用的方法。同样输出一下jQuery.prototype
jQuery.prototype

$(’.box’).addClass()就是找的jQuery.prototype中的addClass方法那么我们就可以把
$(’.box’)看作是jQuery的一个实例对象,原型上的属性和方法只能被其所属类或所属类的实例调用。上源码

  1. var
  2. version = "3.5.1",
  3. //这是在$也就是jQuery执行的时候做的操作
  4. jQuery = function jQuery(selector, context) {
  5. // selector:选择器类型 「字符串(选择器/HTML字符串)、函数、DOM元素对象...」
  6. // context:上下文,限制其获取的范围
  7. return new jQuery.fn.init(selector, context);
  8. };
  9. // 原型方法:供实例调用 原型重定向
  10. jQuery.fn = jQuery.prototype = {
  11. jquery: version,
  12. constructor: jQuery,
  13. // ...
  14. };
  15. //这两段代码意思是一样的
  16. var jQuery.fn = jQuery.prototype;
  17. jQuery.prototype={
  18. jquery: version,
  19. constructor: jQuery
  20. };

上边这段代码表示:原型重定向以后会丢失之前的属性和方法,所以jQuery中自己给新原型加上了jquery和constructor属性。那么在jQuery执行的时候new jQuery.fn.init(selector, context)就是调用的new jQuery.prototype.init(selector, context))也就是原型上的init方法

  1. var rootjQuery = jQuery(document),
  2. rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/,
  3. init = jQuery.fn.init = function (selector, context, root) {
  4. var match, elem;
  5. // HANDLE: $(""), $(null), $(undefined), $(false)
  6. if (!selector) {
  7. return this;
  8. }
  9. // Method init() accepts an alternate rootjQuery
  10. // so migrate can support jQuery.sub (gh-2101)
  11. root = root || rootjQuery;
  12. // Handle HTML strings
  13. if (typeof selector === "string") {
  14. if (selector[0] === "<" &&
  15. selector[selector.length - 1] === ">" &&
  16. selector.length >= 3) {
  17. // Assume that strings that start and end with <> are HTML and skip the regex check
  18. match = [null, selector, null];
  19. } else {
  20. match = rquickExpr.exec(selector);
  21. }
  22. // Match html or make sure no context is specified for #id
  23. if (match && (match[1] || !context)) {
  24. // HANDLE: $(html) -> $(array)
  25. if (match[1]) {
  26. context = context instanceof jQuery ? context[0] : context;
  27. // Option to run scripts is true for back-compat
  28. // Intentionally let the error be thrown if parseHTML is not present
  29. jQuery.merge(this, jQuery.parseHTML(
  30. match[1],
  31. context && context.nodeType ? context.ownerDocument || context : document,
  32. true
  33. ));
  34. // HANDLE: $(html, props)
  35. if (rsingleTag.test(match[1]) && jQuery.isPlainObject(context)) {
  36. for (match in context) {
  37. // Properties of context are called as methods if possible
  38. if (isFunction(this[match])) {
  39. this[match](context[match]);
  40. // ...and otherwise set as attributes
  41. } else {
  42. this.attr(match, context[match]);
  43. }
  44. }
  45. }
  46. return this;
  47. // HANDLE: $(#id)
  48. } else {
  49. elem = document.getElementById(match[2]);
  50. if (elem) {
  51. // Inject the element directly into the jQuery object
  52. this[0] = elem;
  53. this.length = 1;
  54. }
  55. return this;
  56. }
  57. // HANDLE: $(expr, $(...))
  58. } else if (!context || context.jquery) {
  59. return (context || root).find(selector);
  60. // HANDLE: $(expr, context)
  61. // (which is just equivalent to: $(context).find(expr)
  62. } else {
  63. return this.constructor(context).find(selector);
  64. }
  65. // HANDLE: $(DOMElement)
  66. } else if (selector.nodeType) {
  67. this[0] = selector;
  68. this.length = 1;
  69. return this;
  70. // HANDLE: $(function)
  71. // Shortcut for document ready
  72. } else if (isFunction(selector)) {
  73. return root.ready !== undefined ?
  74. root.ready(selector) :
  75. // Execute immediately if ready is not present
  76. selector(jQuery);
  77. }
  78. return jQuery.makeArray(selector, this);
  79. };
  80. //init的原型指向重定向以后的原型
  81. init.prototype = jQuery.fn;

基于原型和原型链的图:
调用时创建实例对象

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jQuery分析

    一、jQuery如何做到不污染变量名并暴露出 $ 供用户使用 jQuery将变量和代码写进立即执行函数,通过函数来包裹所有的变量和方法,再在这个立即执行函数上将 jQuery

    相关 jQuery学习

    学习前端也有一段时间了,闲暇之余决定进一步学习,从jquery源码学习开始。 嗯,打开jquery.js,嗯,太多了,一脸懵! 一步一步来吧! 刚开始看了[艾伦 Aaro