1 Common JS 介绍

Common JS 是模块化规范之一。每个文件都是一个作用域,文件里面定义的变量/函数都是私有的,对其他模块不可见。Common JS 规范在 Node 端和浏览器端有不同的实现。

1.1 暴露模块

暴露模块有两种方式:module.exportexports ,两种方式均可以暴露一个函数或对象。两种方式本质上是一样的,Common JS 在每个模块中隐式将 module.exports 指向(赋值)给 exports 语法格式如下:

// 暴露函数
module.exports = function () {} // 暴露对象
module.exports = {
xxx: () => {}
} exports.xxx = {} exports.xxx = function() {}

1.2 加载模块

加载模块使用 require() 函数。格式如下:

const xxx = require('xxxx')

加载模块是同步操作,按照在代码中出现的顺序进行加载。

可以在代码中多次使用 require 加载模块,但只会在首次加载时真正去加载,加载后就会将该模块缓存。

2 Common JS 规范的 Node 实现

Node.js 实现了 Common JS 规范,所以在 Node 环境下可以直接使用 Common JS 规范,无须引入其他包。

2.1 创建模块

创建 modules 目录,在该目录下创建四个文件:module1.jsmodule2.jsmodule3.jsmodule4.js 分别代表 4 个模块。

module1.js 使用 module.exports 暴露一个匿名对象:

const msg = 'this is module1'

console.log(msg)

module.exports = {
testFun: () => {
console.log('in module1 test function.')
}
}

module2.js 使用 module.exports 暴露一个函数:

const msg = 'this is module2'

console.log(msg)

const testFun = () => {
console.log('in module2 test function.')
} module.exports = testFun

module3.js 使用 exports 暴露一个函数:

const msg = 'this is module3'

console.log(msg)

exports.testFun = () => {
console.log('in module3 test function.')
}

module4.js 使用 exports 暴露对象:

const msg = 'this is module4'

console.log(msg)

exports.demo = {
testFun: () => {
console.log('in module4 test function.')
}
}

2.2 使用模块

module 目录同级创建入口 JS 文件 index.js,在该文件中加载并使用上面 4 个模块:

console.log('---- 加载模块 ----')

const demo1 = require('./modules/module1')
const demo2 = require('./modules/module2')
const demo3 = require('./modules/module3')
const demo4 = require('./modules/module4') console.log('---- 使用模块 ----') demo1.testFun()
demo2()
demo3.testFun()
demo4.demo.testFun()

需要注意:使用模块时,要与暴露模块对应起来。

2.3 运行程序

在 Node 环境下运行 index.js

在控制台中输入如下命令:

node ./index.js

控制台输出:

3 Common JS 规范的浏览器实现

3.1 创建 HTML

module 目录同级创建入口 HTML 文件:index.html,在该文件中通过 script 标签引入上面编写的 index.js 文件:

<script src="./index.js"></script>

在浏览器中访问 index.html ,会发现浏览器的 console 中提示如下错误:

这是因为浏览器不认识 require ,所以需要使用工具将 Common JS 规范的代码编译为浏览器识别的 JS 语法。这里咱们使用 browserify

3.2 browserify

browserify 可以支持咱使用 Common JS 模块化规范来组织浏览器端的 Javascript 代码。

全局安装 browserify

npm install -g browserify

查看 browserify 版本号:

browserify --version

使用 browserify 编译 Common JS 规范的代码:

browserify ./index.js -o ./bundle.js

执行该命令后,会在当前目录下生成 bundle.js 文件。

index.html 文件中引入 bundle.js

<script src="./bundle.js"></script>

3.3 运行HTML

再次在浏览器中访问 index.html,此时在浏览器控制台中会输出正确的结果:

4 总结

Common JS 规范的语法:

  • 暴露模块:module.exportsexports
  • 加载模块: require()

Common JS 规范的使用:

  • Node:Node JS 支持 Common JS 规范;
  • 浏览器:需要使用 browserify 编译。

感谢你阅读本文,如果本文给了你一点点帮助或者启发,还请三连支持一下,点赞、关注、收藏,作者会持续与大家分享更多干货

JS 模块化 - 02 Common JS 模块化规范的更多相关文章

  1. 常用js方法整理common.js

    项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...

  2. 项目中常用js方法整理common.js

    抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...

  3. (六)Net Core项目使用Controller之一 c# log4net 不输出日志 .NET Standard库引用导致的FileNotFoundException探究 获取json串里的某个属性值 common.js 如何调用common.js js 筛选数据 Join 具体用法

    (六)Net Core项目使用Controller之一 一.简介 1.当前最流行的开发模式是前后端分离,Controller作为后端的核心输出,是开发人员使用最多的技术点. 2.个人所在的团队已经选择 ...

  4. 常用js函数整理--common.js

    var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data: data, dataType: 'json ...

  5. 模块化规范Common.js,AMD,CMD

    随着网站规模的不断扩大,嵌入网页中的javascript代码越来越大,开发过程中存在大量问题,如:协同开发,代码复用,大量文件引入,命名冲突,文件依赖. 模块化编程称为迫切的需求. 所谓的模块,就是实 ...

  6. 02: vue.js常用指令

    目录:Vue其他篇 01: vue.js安装 02: vue.js常用指令 目录: 1.1 vuejs简介 1.2 选择器:根据id.class等查找 1.3 静态绑定数据 data 1.4 插值 { ...

  7. JavaScript模块化-require.js,r.js和打包发布

    在JavaScript模块化和闭包和JavaScript-Module-Pattern-In-Depth这两篇文章中,提到了模块化的基本思想,但是在实际项目中模块化和项目人员的分工,组建化开发,打包发 ...

  8. JS模块化工具require.js教程(二):基本知识

    前一篇:JS模块化工具我们以非常简单的方式引入了requirejs,这一篇将讲述一下requirejs中的一些基本知识,包括API使用方式等 基本API require会定义三个变量:define,r ...

  9. Javascript模块化工具require.js教程

    转自:http://www.w3cschool.cc/w3cnote/requirejs-tutorial-1.html, http://www.w3cschool.cc/w3cnote/requir ...

随机推荐

  1. springboot配置logback.xml

    由于springboot框架自带log4j,因此我们只需配置下logback文件,即可, 在main/resources根目录下,新建logback-spring.xml文件,copy下述代码: &l ...

  2. XJSON 是如何实现四则运算的?

    前言 在上一篇中介绍了 xjson 的功能特性以及使用查询语法快速方便的获取 JSON 中的值. 同时这次也更新了一个版本,主要是两个升级: 对转义字符的支持. 性能优化,大约提升了30%️. 转义字 ...

  3. 使用xpath查找元素的子元素,找不到

    List<WebElement> allUserList = driver.findElements(By.xpath("xxx")); for (WebElement ...

  4. Java双重校验单例模式详解

    单例模式双重检测java实现: public class Singleton { private volatile static Singleton instance = null; //#1 pub ...

  5. 科学计算库Numpy基础&提升(理解+重要函数讲解)

    Intro 对于同样的数值计算任务,使用numpy比直接编写python代码实现 优点: 代码更简洁: numpy直接以数组.矩阵为粒度计算并且支持大量的数学函数,而python需要用for循环从底层 ...

  6. Codeforces Round #801 (Div. 2) and EPIC Institute of Technology Round(C,D题解)

    Codeforces Round #801 (Div. 2) and EPIC Institute of Technology Round C - Zero Path 在这道题目中,不可以真正地进行寻 ...

  7. UE4.25 Slate源码解读

    概述 Slate系统是UE的一套UI解决方案,UMG系统也是依赖Slate系统实现的. 问题: Slate系统是如何组织的? 控件树的父子关系是如何绑定的? Slate系统是如何渲染的? slate渲 ...

  8. wamp升级php

    1.  停止WAMP服务器. 2.  去网站windows.php.net 下载php-5.4.31-nts-Win32-VC9-x86.zip(32位的). 不要下载THE INSTALLER. 3 ...

  9. YII地址切换

    以/开头表示跳出当前控制器 例如 return $this->render('/code/login'// 跳出当前控制器,进入Code下login视图 ,['model' => $mod ...

  10. AI目标分割能力,无需绿幕即可实现快速视频抠图

    绿幕抠图是影视制作过程中常见的技术手段,常用于视频中抠除并替换背景,通过后期加工实现视频剪辑制作的更多可能性.然而,绿幕抠图技术制作成本费时费力,无法应用于日常生活. 华为视频编辑服务近期上线目标分割 ...