{"id":16385793,"url":"https://github.com/aaaaash/jquery-source-code","last_synced_at":"2026-02-12T14:35:31.282Z","repository":{"id":100795489,"uuid":"61628148","full_name":"Aaaaash/jquery-source-code","owner":"Aaaaash","description":"jquery源码学习","archived":false,"fork":false,"pushed_at":"2016-06-22T01:06:08.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-10T16:48:18.248Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Aaaaash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-21T11:20:50.000Z","updated_at":"2016-06-21T11:20:50.000Z","dependencies_parsed_at":"2023-06-10T01:02:45.447Z","dependency_job_id":null,"html_url":"https://github.com/Aaaaash/jquery-source-code","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Aaaaash/jquery-source-code","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aaaaash%2Fjquery-source-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aaaaash%2Fjquery-source-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aaaaash%2Fjquery-source-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aaaaash%2Fjquery-source-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aaaaash","download_url":"https://codeload.github.com/Aaaaash/jquery-source-code/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aaaaash%2Fjquery-source-code/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29368707,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-12T08:51:36.827Z","status":"ssl_error","status_checked_at":"2026-02-12T08:51:26.849Z","response_time":55,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T04:15:22.917Z","updated_at":"2026-02-12T14:35:31.266Z","avatar_url":"https://github.com/Aaaaash.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"#jquery源码学习笔记\n##整体架构\n\u003ejQuery框架的核心就是从HTML文档中匹配元素并对其执行操作\u003cbr/\u003e\n$('.someelement').css()\u003cbr/\u003e\n$('.someelement').find()...等\u003cbr/\u003e\n\njQuery有两大特点\u003cbr/\u003e\n* jquery对象的构建方式\n* jquery方法调用方式\n\n###jquery的无new构建\nJavaScript是函数式语言，函数可以实现`类`（class）,类就是面向对象中最基本的概念\u003cbr/\u003e\n\n    var aQuery=function(selector,context){\n        //构造函数\n    }\n    aQuery.prototype={\n        //原型\n        name:function(){},\n        age:function(){}\n    }\n    var a=new aQuery();\n    a.name();\n\n这是JavaScript中对象最常规的使用方法，但明显jquery不是这样做的\u003cbr/\u003e\njquery没有直接使用new运算符实例化jquery对象\u003cbr/\u003e\n\n####如何返回一个实例？\nJavaScript中实例this只跟原型有关\u003cbr/\u003e\n把jquery类当做一个工厂方法来创建实例，把这个方法放到jquery. prototype原型中\u003cbr/\u003e\n\n    var aQuery=function(selector,context){\n        return aQuery,prototype.init();\n    }\n    aQuery.prototype={\n        init:function(){\n            return this;\n        },\n        name:function(){},\n        age:function(){}\n    }\n\n此时再执行aQuery()则会返回一个实例\u003cbr/\u003e\n但是这样写会有一个问题，init中的this指向的是aQuery类，如果把init函数也看做一个构造函数，其内部的this就指向错误了\u003cbr/\u003e\n所以要给这个this设计出独立的作用域才行\u003cbr/\u003e\n\n####jquery框架分隔作用域\n    var aQuery=function(selector,context){\n        return new aQuery.prototype.init();\n    }\n    aQuery.prototype={\n        init:function(){\n            this.age=18;\n            return this;\n        },\n        name:function(){},\n        age:function(){}\n    }\n    console.log(aQuery().name());\n\n再次执行后会抛出一个错误，因为aQuery实际上返回了一个其原型中init构造函数的实例，而init上并没有name这个方法\u003cbr/\u003e\n这里init就形成了一个独立的作用域，和aQuery的this分离了\u003cbr/\u003e\n\n####怎么访问jquery类原型上的属性与方法？\n虽然上面分离了init与aQuery的this，但是aQuery实例化出的对象确无法访问它原型中的方法了\u003cbr/\u003e\n如何做到既能隔离作用域还能让aQuery实例化对象可以访问到aQuery原型对象？\u003cbr/\u003e\n可以将aQuery的原型覆盖init构造函数的原型\u003cbr/\u003e\n    var aQuery=function(selector,context){\n        return new aQuery.prototype.init();\n    }\n    aQuery.prototype={\n        init:function(){\n            return thhis;\n        },\n        name:function(){\n            return this.age;\n        },\n        age:20\n    }\n    aQuery.prototype.init.prototype=aQuery.prototype;           //关键\n    console.log(aQuery().name());           //20,成功访问到了aQuery的原型\n\n###链式调用\njquery可以使用链式调用，节省了不必要的js代码，提高代码执行效率\u003cbr/\u003e\n\u003eaQuery().init().name()...\n\n很明显链式调用的基本条件就是实例的this，也就是说每个方法最终返回的都是它们所属的实例对象，则可以调用它自身的所有方法\u003cbr/\u003e\n\n    aQuery.prototype={\n        init:function(){\n            //....\n            return this;\n        },\n        name:function(){\n            //....\n            return this;\n        }\n    }\n\n这样我们就可以使用链式调用了，因为每次方法执行完会返回实例对象本身（this），从而实例对象又可以访问自己的属性了\u003cbr/\u003e\n\n###插件接口\n\u003c!--  --\u003e\n\n##选择器\njquery框架的基础就是查询，查询dom元素\u003cbr/\u003e\n###jquery选择器接口\njquery是总入口，选择其一共支持9种方式\u003cbr/\u003e\n    $(document)\n    $('\u003cdiv\u003e')\n    $('div')\n    $('#test')\n    $(function(){})\n    $('input:radio',document.forms[0]);\n    $('input',$('div'))\n    $()\n    $('\u003cdiv\u003e',{\n        'class':'test',\n        text:'click me',\n        click:function(){$(this).toggleClass('test')}\n        }).appendTo('body');\n    $($('.test'));\n\n###jquery查询的对象是dom元素，查询到目标元素后如何存储？\n* 查询得到结果储存到jquery对象内部，由于查询的dom可能是单个元素，也有可能是一个合集\n* jquery内部应该要定义一个合集数组用于存储选择后的元素\n* 根据API，jquery构建的不仅仅是dom元素，还有html字符串，Object等等\n\n内部会按照一些特定类型的值来处理$()中传入的参数，例如处理“”，null，undefined，false等直接返回this\u003cbr/\u003e\n处理字符串（之后根据选择器是class还是id以及其他分别处理）\u003cbr/\u003e\n处理dom元素\u003cbr/\u003e\n处理function\u003cbr/\u003e\n\n####匹配id选择器：$('#id')\n1.首先进入字符串处理\u003cbr/\u003e\n    if(typeof selector==='string'){\n        //...\n    }\n2.如果发现是以'\u003c'开始'\u003e'结尾的dom元素，例如 $('\u003cp id=\"test\"\u003eMy \u003cem\u003enew\u003c/em\u003e text\u003c/p\u003e')这种情况，进入正则检查\u003cbr/\u003e\n    rquickExpr = /^(?:\\s*(\u003c[\\w\\W]+\u003e)[^\u003e]*|#([\\w-]*))$/;         //用于判断是否为html标签的正则表达式\u003cbr/\u003e\n    match=requickExpr.exec(selector);\n3.开始匹配html标签\n    if(match\u0026\u0026(match[1]||!context)){\n        if(match[1]){\n            //...\n        }\n    }\n4.处理ID\n    elem=document.getElementById(match[2]);\n    if(elem \u0026\u0026 elem.parentNode){\n        this.length=1;\n        this[0]=elem;\n    }\n    this.context=document;\n    this.selector=selectorl\n    return this;\n其中this就是jquery工厂化后返回的实例对象\u003cbr/\u003e\n\n####匹配className选择器:$('.className')\n如果第一个参数是一个类名，jquery对象中拥有class名为clasName的标签元素，可以直接使用find方法\u003cbr/\u003e\n    return jQuery(document).find(className);    //传入上下文\n####匹配$('.className,context')\n第一个参数是类名，第二个参数是一个上下文对象（比如className，dom节点等）\u003cbr/\u003e\n    return jQuery(context).find(className)\n等等....\u003cbr/\u003e\n\n###jquery构造器\n本质上来说，构建的jquery对象，其实不仅仅只是dom，还有很多附加的元素，用数组的方式存储\u003cbr/\u003e\n总的来说分为两大类：\u003cbr/\u003e\n* 单个dom元素，例如$(id)，则直接把dom元素作为数组传递给this对象\n* 多个dom元素，可以通过css选择器匹配dom元素，构建数据结构\n\ncss选择器是通过jquery.find(selector)函数完成的，通过它可以分析选择器字符串，并在dom文档树种查找符合语法的元素集合\u003cbr/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaaaash%2Fjquery-source-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faaaaash%2Fjquery-source-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaaaash%2Fjquery-source-code/lists"}