commonjs

作者: 手指乐 | 来源:发表于2019-10-07 16:26 被阅读0次
  • commonJS概述
    1.文件即为模块
    每个文件都是一个作用域,文件里面定义的变量\函数都是私有的,
    对其他模块都是不可见的;

2.使用module.exports (exports) 暴露对外的接口.
module变量代表当前模块,module是一个对象,使用这个对象属性exports 暴露对外的接口;

3.使用require 同步加载依赖模块
4.模块可以多次加载,但是只会在第一次加载时运行一次,然后运行结果就被缓存了,以后再加载,就直接读取缓存结果。要想让模块再次运行,必须清除缓存。

5.模块加载的顺序,按照其在代码中出现的顺序

6.每个模块内部,都有一个module对象,代表当前模块。它有以下属性。

7.module.exports属性
module.exports属性表示当前模块对外输出的接口,其他文件加载该模块,实际上就是读取module.exports变量。

8.exports变量
为了方便,Node为每个模块提供一个exports变量,指向module.exports。这等同在每个模块头部,有一行这样的命令:
var exports = module.exports;(commonJS隐式做了这个赋值)
这样做的好处是,在对外输出模块接口时,可以向exports对象添加方法暴露出去。

  • 示例
    util.js
module.exports = function say() {
    console.log('hello world');
    console.log(module.id) //模块的识别符,通常是带有绝对路径的模块文件名。
    console.log(module.loaded) //返回一个布尔值,表示模块是否已经完成加载。
    console.log(module.children) //返回一个数组,表示该模块要用到的其他模块。
    console.log(module.exports) //表示模块对外输出的值。
}

main.js

var say = require("./util");
say();

也可以这样写:
util.js

function say(){
    console.log('hello world');
    console.log(module.id) //模块的识别符,通常是带有绝对路径的模块文件名。
    console.log(module.loaded) //返回一个布尔值,表示模块是否已经完成加载。
    console.log(module.children) //返回一个数组,表示该模块要用到的其他模块。
    console.log(module.exports) //表示模块对外输出的值。
}


module.exports ={
    sayfunc:say,
}

main.js

var util = require("./util");
util.sayfunc();

前者一个模块只输出一个function,后者输出一个对象,里面可以扩展多个方法、属性等
比如:

function say(){
    console.log('hello world');
    console.log(module.id) //模块的识别符,通常是带有绝对路径的模块文件名。
    console.log(module.loaded) //返回一个布尔值,表示模块是否已经完成加载。
    console.log(module.children) //返回一个数组,表示该模块要用到的其他模块。
    console.log(module.exports) //表示模块对外输出的值。
}

function test(){
    console.log('test');
}


module.exports ={
    sayfunc:say,
    testfunc:test
}

也可以使用exports:

exports.sayfunc = function say(){
    console.log('hello world');
    console.log(module.id) //模块的识别符,通常是带有绝对路径的模块文件名。
    console.log(module.loaded) //返回一个布尔值,表示模块是否已经完成加载。
    console.log(module.children) //返回一个数组,表示该模块要用到的其他模块。
    console.log(module.exports) //表示模块对外输出的值。
}

exports.testfunc = function test(){
    console.log('test');
}
  • module.export与exports区别
    // dep.js
    exports.A = function() {}
    // app.js
    var dep = require('dep');
    dep.A()
    这其中exports.A = function() {}也可以写成module.exports.A = function() {}
    // dep.js
    module.exports = function () {}
    // app.js
    var dep = require('dep');
    dep();
    注意这里只能用module.exports
    如果写成
    exports = function () {}
    会让exports跟module.exports的隐式赋值断开,相当于给exports重新赋值
    而最终导出的是module.exports
    而且之后再怎么向exports上挂变量 (如exports.A = 1) 都不会被导出了

相关文章

  • Commonjs规范

    CommonJS和AMD CommonJS Nodejs的模块系统就采用CommonJS模式。CommonJS标准...

  • 3.webpack自动生成项目中的HTML文件

    1. webpack中的CommonJS和ES Mudule 规范 1.1 CommonJs规范 CommonJs...

  • JS模块化

    模块化规范:CommonJS,AMD,CMD,UMD,ES6 Module CommonJS CommonJS是服...

  • CommonJs和ES Module

    什么是CommonJS CommonJS is a project with the goal to establ...

  • js模块化

    CommonJS CommonJS规范是诞生比较早的。NodeJS就采用了CommonJS。是这样加载模块: 这种...

  • node学习2

    什么是CommonJs? CommonJs就是模块化的标准,nodejs就是CommonJs(模块化)的实现 No...

  • JS module 导入与导出(commonJS、ES6)

    commonJS 规范导出、commonJS 规范导入 ES6导出、ES6导入 commonJS 规范导出,ES6...

  • CommonJS

    commonJS 是 node端的引入模块方案 浏览器端实现commonJS步骤 开发环境使用commonjs编码...

  • CommonJS AMD CMD UMD

    CommonJS CommonJS是服务器端模块的规范,Node.js采用了这个规范。 根据CommonJS规范,...

  • CommonJS和AMD

    CommonJS CommonJS是服务器端模块的规范,Node.js采用了这个规范。 根据CommonJS规范,...

网友评论

    本文标题:commonjs

    本文链接:https://www.haomeiwen.com/subject/yoympctx.html