René's URL Explorer Experiment


Title: 01--javascript模块化进程 · Issue #1 · simplexcspp/JavaScript-Module · GitHub

Open Graph Title: 01--javascript模块化进程 · Issue #1 · simplexcspp/JavaScript-Module

X Title: 01--javascript模块化进程 · Issue #1 · simplexcspp/JavaScript-Module

Description: 一、早期javascript模块的基本写法 什么模块? 最简单的理解: 实现特定功能的一组方法 1、模块的原始写法 function m1(){ // your code } function m2(){ // your code } m1和m2组成一个模块,使用时直接调用。这种原始方式的模块形式,缺点很明显: 污染了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关系。 2、模块的对象写法: 针对上面原始写法的缺点,将模块写成一个对象,所有模块...

Open Graph Description: 一、早期javascript模块的基本写法 什么模块? 最简单的理解: 实现特定功能的一组方法 1、模块的原始写法 function m1(){ // your code } function m2(){ // your code } m1和m2组成一个模块,使用时直接调用。这种原始方式的模块形式,缺点很明显: 污染了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关...

X Description: 一、早期javascript模块的基本写法 什么模块? 最简单的理解: 实现特定功能的一组方法 1、模块的原始写法 function m1(){ // your code } function m2(){ // your code } m1和m2组成一个模块,使用时直接调用。这种原始方式的模块形式,缺点很明显: 污染了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关...

Opengraph URL: https://github.com/simplexcspp/JavaScript-Module/issues/1

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"01--javascript模块化进程","articleBody":"### 一、早期javascript模块的基本写法\r\n\r\n\u003e 什么模块?\r\n\u003e 最简单的理解: **实现特定功能的一组方法**\r\n\r\n- 1、模块的原始写法\r\n```\r\nfunction m1(){\r\n  // your code\r\n}\r\n\r\nfunction m2(){\r\n  // your code\r\n}\r\n```\r\n m1和m2组成一个模块,使用时直接调用。这种原始方式的模块形式,缺点很明显:\r\n \r\n\u003e **污染了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关系。**\r\n\r\n- 2、模块的对象写法:\r\n\r\n 针对上面原始写法的缺点,将模块写成一个对象,所有模块成员都放入这个对象里,使用时直接调用此对象的方法。\r\n```\r\nvar myModule = {\r\n            \r\n    _count: 0,\r\n            \r\n    m1: function () {\r\n\r\n        // your code\r\n\r\n    },\r\n            \r\n    m2: function () {\r\n\r\n         // your code\r\n\r\n    }           \r\n};\r\nmyModule .m1();\r\n```\r\n那么,对象写法有什么缺点?很明显:\r\n\r\n\u003e 对象写法会暴露所有模块成员,内部私有属性可以被外部改写。\r\n\r\n下面代码改写了私有属性_count的值。\r\n\r\n```\r\nmyModule._count = 100;\r\n\r\n```\r\n- 3、立即执行函数写法:\r\n\r\n 为了解决对象写法暴露所有模块成员的问题,使用立即执行函数写法,则不会暴露私有成员。\r\n\r\n```\r\nvar myModule = (function () {  \r\n      \r\n    var _count = 0;\r\n\r\n    function m1 () {\r\n\r\n        // your code\r\n\r\n    }          \r\n\r\n    function m2 () {\r\n\r\n         // your code\r\n\r\n    }           \r\n\r\n    return {\r\n\r\n        m1: m1,\r\n\r\n        m2: m2\r\n\r\n    };\r\n})();\r\n\r\n```\r\n使用立即执行函数的写法,外部代码无法读取内部的_count变量。\r\n\r\n```\r\nconsole.log(myModule._count); // undefined\r\n\r\n```\r\n\u003e 如果一个模块很大,必须分成多个部分,又或者一个模块需要继承另一个模块,那么该如何处理?\r\n\u003e 这时候就要使用**放大模式**,如下:\r\n\r\n- 4、放大模式\r\n\r\n```\r\nvar myModule = (function (m) { \r\n   \r\n    // 给myModule扩展的新方法    \r\n    m.m3 = function () {\r\n        // your code\r\n    };\r\n\r\n    return m;\r\n\r\n})(myModule);\r\n\r\n```\r\n上面的代码为myModule模块添加了一个新方法m3,然后返回新的myModule模块。\r\n\r\n\u003e 在浏览器环境中,如果我们的模块的各个部分都是从网上获取的,有时无法知道哪个部分会先加载。如果采用放大模式的写法,第一个执行的部分有可能加载一个不存在空对象,该如何处理?\r\n\u003e 这时候就要使用**宽放大模式**,如下:\r\n\r\n- 5、宽放大模式\r\n\r\n```\r\n  var myModule = (function (m) {\r\n\r\n    // 给myModule扩展的新方法\r\n    m.m3 = function () {  \r\n    \r\n        // your code   \r\n\r\n    };\r\n\r\n    return m;\r\n\r\n  })(myModule || {});\r\n\r\n```\r\n与\"放大模式\"相比,"宽放大模式"就是立即执行函数的参数可以是空对象。这样,当从网上获取的模块异常时,就不会报错。\r\n\r\n\u003e 一个模块,应保证其独立性,模块内部最好不与程序的其他部分直接交互,如果要在模块内部调用全局变量,最好是显式地将其他变量输入模块。\r\n\r\n- 6、模块内部输入全局变量\r\n\r\n```\r\nvar myModule = (function ($) {\r\n\r\n    // your code\r\n\r\n })(jQuery);\r\n\r\n```\r\n### 二、早期javascript模块的基本写法\r\n\r\n- 1、CommonJS规范背景简述\r\n\r\n早期,一开始大家都认为JS是辣鸡,没什么用,官方定义的API只能构建基于浏览器的应用程序。 随后,CommonJS API定义很多普通应用程序(主要是非浏览器的应用)使用的API,从而填补了这个空白。它的终极目标是提供一个类似Python,Ruby和Java标准库。这样的话,开发者可以使用CommonJS API编写应用程序,然后这些应用可以运行在不同的JavaScript解释器和不同的主机环境中。\r\n    \r\n在兼容CommonJS的系统中,你可以使用JavaScript开发以下程序:\r\n\r\n-  服务器端JavaScript应用程序\r\n -  命令行工具\r\n-  图形界面应用程序\r\n-  混合应用程序(如,Titanium或Adobe AIR)\r\n\r\n2009年,美国程序员Ryan Dahl创造了**node.js**项目,将javascript语言用于服务器端编程,这标志Javascript模块化编程正式诞生。\r\n\r\n在浏览器环境下,没有模块也不是特别大的问题,毕竟网页程序的复杂性有限;但是在服务器端,一定要有模块,与操作系统和其他应用程序互动,否则根本没法编程。NodeJS是CommonJS规范的实现,webpack 也是以CommonJS的形式来书写。 **node.js的模块系统,就是参照CommonJS规范实现的**。\r\n\r\n- 2、CommonJS规范简介\r\n\r\n在CommonJS中,有一个全局性方法`require()`,用于加载模块。假定有一个模块为`math.js`,就可以像下面这样加载:\r\n\r\n```\r\nvar math = require('math');\r\n\r\n```\r\n\r\n 然后,就可以调用模块提供的方法:\r\n\r\n```\r\nvar math = require('math');\r\nmath.add(2,3); \r\n```\r\n**CommonJS定义的模块分为**:\r\n\r\n  - 模块引用require\r\n  - 模块定义exports\r\n  - 模块标识module\r\n\r\n\u003e require用来引入外部模块;exports对象用于导出当前模块的属性和方法,是唯一的出口;module对象代表模块本身。\r\n\r\n- 3、AMD规范背景\r\n\r\n基于commonJS规范的nodeJS出来以后,服务端的模块概念已经形成,很自然地,大家就想要客户端模块。而且最好两者能够兼容,一个模块不用修改,在服务器和浏览器都可以运行。但是,由于一个重大的局限,使得CommonJS规范不适用于浏览器环境。还是上面的代码,如果在浏览器中运行,会有一个很大的问题。什么问题?\r\n\r\n```\r\nvar math = require('math');\r\nmath.add(2,3); \r\n\r\n```\r\n\r\n第二行`math.add(2, 3)`,在第一行`require('math')`之后运行,因此必须等`math.js`加载完成。也就是说,如果加载时间很长,整个应用就会停在那里等,这对服务器端不是一个问题,因为所有的模块都存放在本地硬盘,可以同步加载完成,等待时间就是硬盘的读取时间。但是,对于浏览器,这却是一个大问题,因为模块都放在服务器端,等待时间取决于网速的快慢,可能要等很长时间,浏览器处于\"**假死**\"状态。\r\n\r\n 浏览器端的模块,不能采用\"**同步加载**\"(synchronous),只能采用\"**异步加载**\"(asynchronous),这就是AMD规范诞生的背景。其是 **RequireJS**在推广过程中对模块定义的规范化产出。\r\n\r\n\u003e CommonJS是主要为了JS在服务端的表现制定的,他是不适合客户端(浏览器)的,AMD(异步模块定义)出现了,它就主要为客户端JS的表现制定规范。\r\n\r\n- 4、AMD规范简介\r\n\r\n AMD是\"Asynchronous Module Definition\"的缩写,意思就是\"异步模块定义\"。它采用异步方式加载模块,模块的加载不影响它后面语句的运行。**所有依赖这个模块的语句,都定义在一个回调函数中,等到加载完成之后,这个回调函数才会运行**。\r\n\r\nAMD也采用`require()`语句加载模块,但是不同于CommonJS,它要求两个参数:\r\n\r\n```\r\nrequire([module], callback);\r\n\r\n```\r\n\r\n第一个参数`[module]`,是一个数组,里面的成员就是要加载的模块;第二个参数`callback`,则是加载成功之后的回调函数。如果将前面的代码改写成AMD形式,就是下面这样:\r\n\r\n```\r\nrequire(['math'], function (math) {\r\n\r\n    math.add(2, 3);\r\n\r\n});\r\n\r\n```\r\n\r\n`math.add()`与`math`模块加载不是同步的,浏览器不会发生假死。所以很显然,AMD比较适合浏览器环境。\r\n\r\n- 5、CMD规范\r\n\r\n 大佬玉伯写了seajs,CMD规范 SeaJS 在推广过程中对模块定义的规范化产出。\r\n\r\n- 6、AMD、CMD规范异同\r\n\r\nAMD(requirejs)和 CMD (seajs)都是模块加载器,倡导模块化开发理念,核心价值是让 JavaScript 的模块化开发变得简单自然。\r\n\r\n区别:\r\n\r\n- 对于依赖的模块,AMD**提前执行**(即时加载),CMD是**延迟执行**(按需加载)\r\n\r\n- CMD推崇**依赖就近**,AMD推崇**依赖前置**,如下:\r\n\r\n```\r\n// CMD\r\ndefine(function(require, exports, module) {   \r\n\r\n    var a = require('./a')   \r\n\r\n    a.doSomething()   \r\n\r\n     // ...\r\n\r\n    var b = require('./b') \r\n\r\n    // 依赖可以就近书写   \r\n\r\n    b.doSomething()  \r\n\r\n    // ... \r\n\r\n})\r\n\r\n// AMD \r\ndefine(['./a', './b'], function(a, b) {  \r\n\r\n    // 依赖必须一开始就写好    \r\n\r\n    a.doSomething()    \r\n\r\n    // ...   \r\n\r\n    b.doSomething()   \r\n     // ...   \r\n })\r\n\r\n```\r\n\r\n- AMD 的 API 默认是一个当多个用,CMD 的 API 严格区分,推崇职责单一。比如 AMD 里,require 分全局 require 和局部 require,都叫 require。CMD 里,没有全局 require,而是根据模块系统的完备性,提供 seajs.use 来实现模块系统的加载启动。CMD 里,每个 API 都简单纯粹。\r\n\r\n**主要参考资料**:\r\n\r\n- [阮大师播客有关模块化介绍](http://www.ruanyifeng.com/home.html)\r\n- [CommonJS规范](http://javascript.ruanyifeng.com/nodejs/module.html#toc0)\r\n- [requirejs](https://github.com/requirejs/requirejs)\r\n- [seajs](https://seajs.github.io/seajs) \r\n- [AMD 和 CMD 的区别](https://www.zhihu.com/question/20351507)\r\n- [前端模块化开发的价值](https://github.com/seajs/seajs/issues/547)\r\n\r\n\r\n","author":{"url":"https://github.com/simplexcspp","@type":"Person","name":"simplexcspp"},"datePublished":"2018-02-24T13:28:34.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1/JavaScript-Module/issues/1"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:2f098527-6758-06da-8751-58f1b759b60f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCCB4:F135E:2CA435:3C0647:6A6270DA
html-safe-noncef00e05384b7c8a4ec78d6eb7d2551a859dbc2c9c128f09e5e33bd5040a5ad3b9
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0I0OkYxMzVFOjJDQTQzNTozQzA2NDc6NkE2MjcwREEiLCJ2aXNpdG9yX2lkIjoiNTY4MTY5MzM2MTU1ODQxNzYyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac532a71c7f0b347428427007fd369a596725a30b9b2a9efe10e417a07434e3710
hovercard-subject-tagissue:299941187
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/simplexcspp/JavaScript-Module/1/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ec2c758755cc5c1feff9920a6430e4a4bc8656753b386a3f01fa970528877034/simplexcspp/JavaScript-Module/issues/1
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ec2c758755cc5c1feff9920a6430e4a4bc8656753b386a3f01fa970528877034/simplexcspp/JavaScript-Module/issues/1
og:image:alt一、早期javascript模块的基本写法 什么模块? 最简单的理解: 实现特定功能的一组方法 1、模块的原始写法 function m1(){ // your code } function m2(){ // your code } m1和m2组成一个模块,使用时直接调用。这种原始方式的模块形式,缺点很明显: 污染了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesimplexcspp
hostnamegithub.com
expected-hostnamegithub.com
None19769ef9d383a84eeab48bb9b309078c20f4532b73eae2f9713b18406513e0ad
turbo-cache-controlno-preview
go-importgithub.com/simplexcspp/JavaScript-Module git https://github.com/simplexcspp/JavaScript-Module.git
octolytics-dimension-user_id24514952
octolytics-dimension-user_loginsimplexcspp
octolytics-dimension-repository_id122742245
octolytics-dimension-repository_nwosimplexcspp/JavaScript-Module
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id122742245
octolytics-dimension-repository_network_root_nwosimplexcspp/JavaScript-Module
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release76dc6b11f13eaeec1329217c9f16c262ade27a1e
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/simplexcspp/JavaScript-Module/issues/1#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsimplexcspp%2FJavaScript-Module%2Fissues%2F1
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsimplexcspp%2FJavaScript-Module%2Fissues%2F1
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=simplexcspp%2FJavaScript-Module
Reloadhttps://github.com/simplexcspp/JavaScript-Module/issues/1
Reloadhttps://github.com/simplexcspp/JavaScript-Module/issues/1
Reloadhttps://github.com/simplexcspp/JavaScript-Module/issues/1
simplexcspp https://github.com/simplexcspp
JavaScript-Modulehttps://github.com/simplexcspp/JavaScript-Module
Notifications https://github.com/login?return_to=%2Fsimplexcspp%2FJavaScript-Module
Fork 0 https://github.com/login?return_to=%2Fsimplexcspp%2FJavaScript-Module
Star 7 https://github.com/login?return_to=%2Fsimplexcspp%2FJavaScript-Module
Code https://github.com/simplexcspp/JavaScript-Module
Issues 2 https://github.com/simplexcspp/JavaScript-Module/issues
Pull requests 0 https://github.com/simplexcspp/JavaScript-Module/pulls
Actions https://github.com/simplexcspp/JavaScript-Module/actions
Projects https://github.com/simplexcspp/JavaScript-Module/projects
Security and quality 0 https://github.com/simplexcspp/JavaScript-Module/security
Insights https://github.com/simplexcspp/JavaScript-Module/pulse
Code https://github.com/simplexcspp/JavaScript-Module
Issues https://github.com/simplexcspp/JavaScript-Module/issues
Pull requests https://github.com/simplexcspp/JavaScript-Module/pulls
Actions https://github.com/simplexcspp/JavaScript-Module/actions
Projects https://github.com/simplexcspp/JavaScript-Module/projects
Security and quality https://github.com/simplexcspp/JavaScript-Module/security
Insights https://github.com/simplexcspp/JavaScript-Module/pulse
01--javascript模块化进程https://github.com/simplexcspp/JavaScript-Module/issues/1#top
https://github.com/simplexcspp
simplexcspphttps://github.com/simplexcspp
on Feb 24, 2018https://github.com/simplexcspp/JavaScript-Module/issues/1#issue-299941187
阮大师播客有关模块化介绍http://www.ruanyifeng.com/home.html
CommonJS规范http://javascript.ruanyifeng.com/nodejs/module.html#toc0
requirejshttps://github.com/requirejs/requirejs
seajshttps://seajs.github.io/seajs
AMD 和 CMD 的区别https://www.zhihu.com/question/20351507
前端模块化开发的价值https://github.com/seajs/seajs/issues/547
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.