(function(){ var jQuery = function() { // 函数体 } window.jQuery = window.$ = jQuery; })(); console.log(jQuery);
输出结果
上面的空函数就是所谓的构造函数,构造函数在面向对象语言中是类的一个基本方法。
(function(){ var jQuery = function() { // 函数体 } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", test: function() { console.log('test'); } } window.jQuery = window.$ = jQuery; })(); (new jQuery()).test();
上面的方法必须使用下面的方法才能进行调用,这样就会产生很多对象,从而浪费内存消耗。
(new jQuery()).test();
jQuery源码使用了很柔和的方法,也是大家比较熟悉的工厂方法,进行调用。
(function(){ var jQuery = function() { // 函数体 return jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", init: function() { return this; }, test: function() { console.log('test'); } } window.jQuery = window.$ = jQuery; })(); jQuery().test();
假想1:让jQuery函数体直接返回该对象——我用this
(function(){ var jQuery = function() { return this; } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", test: function() { console.log('test'); } } window.jQuery = window.$ = jQuery; })(); console.log(jQuery());
输出结果
发现这里的this指向Window对象。
假想2:让jQuery函数体直接返回类的实例。
(function(){ var jQuery = function() { return new jQuery(); } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", test: function() { console.log('test'); } } window.jQuery = window.$ = jQuery; })(); console.log(jQuery());
输出结果
发现上面是一个递归死循环,出现内存外溢。
思考1:init()方法返回的this作用域是什么?
(function(){ var jQuery = function() { // 函数体 return jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", init: function() { this.init_jquery = '2.0'; return this; } } window.jQuery = window.$ = jQuery; })(); console.log(jQuery().jquery); console.log(jQuery().init_jquery);
输出结果
init()方法中的this作用域:this关键字引用了init()函数作用域所在的对象,同时也能够访问上一级对象jQuery.fn对象的作用。——这种思路会破坏作用域的独立性,对于jQuery框架来说,很可能造成消极影响。
思考2:怎么把init()中的this从jQuery.fn对象中分隔出来?——实例化init初始化类型。
(function(){ var jQuery = function() { // 函数体 return new jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", init: function() { this.init_jquery = '2.0'; return this; } } window.jQuery = window.$ = jQuery; })(); console.log(jQuery().jquery); console.log(jQuery().init_jquery);
输出结果
通过实例化init()初始化类型,限定了init()方法里的this,只在init()函数内活动,不让它超出范围。
jQuery.fn.init.prototype = jQuery.fn;
全部代码:
(function(){ var jQuery = function() { // 函数体 return new jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { // 扩展原型对象 jquery: "1.8.3", init: function() { this.init_jquery = '2.0'; return this; } } jQuery.fn.init.prototype = jQuery.fn; window.jQuery = window.$ = jQuery; })(); console.log(jQuery().jquery); console.log(jQuery().init_jquery);
输出结果
妙棋
把init()对象的prototype指针指向jQuery.fn。——这样init()里的this继承了jQuery.fn原型对象定义的方法和属性。

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库