当前位置:网站首页>JQ源码分析(环境处理)
JQ源码分析(环境处理)
2022-07-30 04:07:00 【前端 贾公子】
JQ可以在哪运行
浏览器 手机端app中(有window环境,不支持commonJS规范,支持es6 module规范)
node环境(没有window 支持commonJS 但不支持Es6Module)
可以用webpack进行编译
支持window 也支持commonJS规范,支持Es6Module规范(可以让Es6Module和commonJS结合)基于node进行打包编译 打包后的结果交给浏览器进行渲染
源码解析
// 自执行函数 保护变量不受全局污染
(function (global, factory) {
/*global:window(浏览器 webpack) / global(this)
factory:回调函数
*/
'use strict'
//当前运行的环境支持Commonjs规范(node&&webpack)
if (typeof module === "object" && typeof module.exports === "object") {
// webpack环境下导出
// module.exports=factory(window,true)
//node 环境下(不支持jq)
// module.exports=function(w){....}
// 使用的时候 let $=require('jquery')-->$()-->报错
module.exports = global.document ?
factory(global, true) :
function (w) {
// node环境下没有document 直接报错
if (!w.document) {
throw new Error("jQuery requires a window with a document");
}
return factory(w);
};
} else {
//当前不支持 commonJS规范 (浏览器环境)
factory(global);
}
})(
typeof window !== 'undefined' ? window : this,
function (window, noGlobal) {
'use strict'
//浏览器环境下导入jq window->window onGlobal->undefined
//webpack环境下 window--》window onGlobal--》true,把factory 执行返回结果 module.exports导出
var version = "3.6.0",
jQuery = function (selector, context) {
};
// 暴露 api
// 当前环境下支持AMD模块思想 导入了jquire.main.js.此时我们使用AMD思想定义jq模块
// 使用 前置导入 require(['jquery'],function($){$()})
if (typeof define === "function" && define.amd) {
define("jquery", [], function () {
return jQuery;
});
}
// 浏览器中执行 在window全局挂载jQuery 使用$()或者jQuery()
if (typeof noGlobal === "undefined") {
window.jQuery = window.$ = jQuery;
}
// 在webpack环境下导出jquery module.export=jquery
// 使用 const $=rquire('jquery')--->$()
return jQuery
}
)仿照环境区分
(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
}
// 暴露api
if (typeof define === "function" && define.amd) {
define("utils", [], function () {
return utils;
});
}
if (typeof noGlobal === "undefined") { window.utils = utils }
return utils
})简便写法
(function () {
'use strict'
let utils = {
version: "1.0.0"
}
// 暴露api
if (typeof module === "object" && typeof module.exports === "object") module.exports = utils
if (typeof window !== 'undefined') window.utils = utils
})()边栏推荐
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions
- CMake installation and testing
- [Node accesses MongoDB database]
- Redis "super explanation!!!!!!"
- How to Effectively Conduct Retrospective Meetings (Part 1)?
- MySQL data query (subtotal and sorting)
- Flutter records and learns different animations (1)
- 【驱动】udev为USB转4串口的每个串口起别名
- redis分布式锁的原子保证
- Roperties class configuration file & DOS to view the host network situation
猜你喜欢

弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升

国内首家沉浸式高逼真元宇宙,希元宇宙正式上线

Eureka Registry

Pytorch框架学习记录2——TensorBoard的使用
![[Node accesses MongoDB database]](/img/00/41000fc7a038f4d46151ab479174b9.png)
[Node accesses MongoDB database]

小程序毕设作品之微信积分商城小程序毕业设计成品(6)开题答辩PPT
![[Switch] Protocol-Oriented Programming in Swift: Introduction](/img/7a/6db558231a77ad9e739b571cf328d6.png)
[Switch] Protocol-Oriented Programming in Swift: Introduction

Problems caused by List getting the difference

小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告

Why is the Kirin 9000 5G version suddenly back in stock?
随机推荐
为什么突然间麒麟 9000 5G 版本,又有库存了?
STM32 SPI+WM8978语音回环
Eureka Registry
Reverse Theory Knowledge 3 [UI Modification]
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report
SQL introduction of the first lecture -- MySQL 8.0.29 installation tutorial (Windows 64 - bit)
Mysql version upgrade, copy the Data file directly, the query is very slow
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview
MySQ deadlock
数组和结构体
The difference between BGP room and ordinary room in Beijing
Advanced Microservices Cloud Alibaba
mysql 结构、索引详解
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
Tcp programming
spicy (two) unit hooks
第51篇-知乎请求头参数分析【2022-07-28】
Eureka注册中心