当前位置:网站首页>JQ source code analysis (environment)
JQ source code analysis (environment)
2022-07-30 04:10:00 【Front end Jia Gongzi】
Where can JQ run
Browser mobile app (with window environment, does not support commonJS specification, supports es6 module specification)
node environment (no window supports commonJS but not Es6Module)
Can be compiled with webpack
Support window and commonJS specification, support Es6Module specification (Es6Module and commonJS can be combined) package and compile based on node, and the packaged result is handed over to the browser for rendering
Source code analysis
// Self-executing function to protect variables from global pollution(function (global, factory) {/*global:window(browser webpack)/global(this)factory: callback function*/'use strict'//The current running environment supports the Commonjs specification (node&&webpack)if (typeof module === "object" && typeof module.exports === "object") {// Export in webpack environment// module.exports=factory(window,true)//node environment (jq is not supported)// module.exports=function(w){....}// When using let $=require('jquery')-->$()-->Errormodule.exports = global.document ?factory(global, true) :function (w) {// There is no document in the node environment and an error is reported directlyif (!w.document) {throw new Error("jQuery requires a window with a document");}return factory(w);};} else {// currently does not support commonJS specification (browser environment)factory(global);}})(typeof window !== 'undefined' ? window : this,function (window, noGlobal) {'use strict'//Import jq in the browser environment window->window onGlobal->undefined//window--"window onGlobal--"true in the webpack environment, export the return result of the factory execution module.exportsvar version = "3.6.0",jQuery = function (selector, context) {};// expose the api// Support the AMD module idea in the current environment and import jquire.main.js. At this time, we use the AMD idea to define the jq module// use pre-import require(['jquery'],function($){$()})if (typeof define === "function" && define.amd) {define("jquery", [], function () {return jQuery;});}// Execute in the browser Mount jQuery globally in the window using $() or jQuery()if (typeof noGlobal === "undefined") {window.jQuery = window.$ = jQuery;}// Export jquery module.export=jquery in webpack environment// use const $=rquire('jquery')--->$()return jQuery})Imitate the environment to distinguish
(function (global, factory) {'use strict'if (typeof module === "object" && typeof module.exports === "object") {module.exports = factory(global, true)} else {factory(global)}})(typeof window !== 'undefined' ? window : this, function (window, noGlobal) {'use strict'let version = "1.0.0",utils = {version}// expose the apiif (typeof define === "function" && define.amd) {define("utils", [], function () {return utils;});}if (typeof noGlobal === "undefined") { window.utils = utils }return utils})Simple writing
(function () {'use strict'let utils = {version: "1.0.0"}// expose the apiif (typeof module === "object" && typeof module.exports === "object") module.exports = utilsif (typeof window !== 'undefined') window.utils = utils})()边栏推荐
- Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
- 一起来学习flutter 的布局组件
- Reverse Theory Knowledge 3 [UI Modification]
- Pytorch框架学习记录5——DataLoader的使用
- sqlmap use tutorial Daquan command Daquan (graphics)
- 小程序毕设作品之微信积分商城小程序毕业设计成品(5)任务书
- Mysql version upgrade, copy the Data file directly, the query is very slow
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
- LeetCode 114. Expand Binary Tree into Linked List (One Question Three Eats)
- Eureka注册中心
猜你喜欢
![[Switch] Protocol-Oriented Programming in Swift: Introduction](/img/7a/6db558231a77ad9e739b571cf328d6.png)
[Switch] Protocol-Oriented Programming in Swift: Introduction

CMake installation and testing

小程序毕设作品之微信积分商城小程序毕业设计成品(6)开题答辩PPT

vscode debugging and remote

小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书

OA Project Pending Meeting & History Meeting & All Meetings

Pytorch框架学习记录3——Transform的使用

WEB 渗透之信息收集

小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要

Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
随机推荐
Taobao/Tmall get Taobao store details API
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
Pytorch框架学习记录1——Dataset类代码实战
[Driver] udev sets the owner, group and permissions after GPIO is loaded
Usage of exists in sql
恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制
智能答题功能,CRMEB知识付费系统必须有!
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions
Based on all volunteers - H and D1 XR806 rare plant monitoring device
The underlying mechanism of the function
Pytorch framework learning record 1 - Dataset class code combat
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview
Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
Pytorch framework learning record 4 - the use of datasets (torchvision.dataset)
STM32 SPI+WM8978语音回环
Alibaba search new product data API by keyword
Hongji was once again shortlisted in the Gartner 2022 RPA Magic Quadrant and achieved a significant jump in position
FreeRTOS Personal Notes - Memory Management
High Concurrency Framework Disruptor