require、module、exports dojo中的三个特殊模块标识

简介:

 查看dojo源码过程中,发现这三个模块名并不以dojo、dijit、dojox开头,在dojo加载器中属于特殊模块名。

  

  

  require

  这是一个上下文智能的加载器。

  我们通过dojoConfig配置了一个package:myApp,现在对myApp目录下的文件可以使用如下方式请求:


// this is "myApp/topLevelHandlers"
define(["dojo"], function(dojo){
  dojo.connect(dojo.byId("debugButton"), "click", function(){
    require(["myApp/perspectives/debug"], function(perspective){
      perspective.open();
    });
  });
});

  通过全局的require函数请求一个myApp包下的文件仍然用“包名+文件路径”的形式。如果我们希望像define那样使用相对路径加载模块:reqiure(['./perspectives/debug'], function(){debug});而require是无法找到“./perspectives/debug”这个module的。这时就轮到这个上下文智能的require模块上场了。


// this is "myApp/topLevelHandlers"
define(["dojo", "require"], function(dojo, require){
  dojo.connect(dojo.byId("debugButton"), "click", function(){
    require(["./perspectives/debug"], function(perspective){
      perspective.open();
    });
  });
});

  通过引用reqire模块代替全局require函数,我们可以使用相对路径来加载模块,这就是上下文智能。

 

exports

  exports这个模块的作用与node.js中exports的作用类似,用于导出当前模块的方法或者变量。在node中它是唯一导出的出口,但是在dojo中我们可以直接在模块中返回一个对象字面量。


define([], function(){
  return {
    someProperty:"hello",
    someOtherProperty:"world"
  };
});


define(["exports"], function(exports){
  exports.someProperty = "hello";
  exports.someOtherProperty = "world";
});

 一般情况下,这两种写法都相同,但是在发生循环依赖情况下,使用exports dojo的加载器可以判断出模块是否加载完毕,而不是出于死锁状态。

 

module

  

  module引用module模块会返回一个对象,该对象有三个参数:
  • id:一个唯一的模块id,在程序中唯一标识一个模块;require([module.id], function(m){}); m代表了该id所对应的模块;这个属性在一些需要需要根据模块id与一个计数器拼接起来的功能时常用到,比如dojo/request/script中callback参数

  • uri:代表该模块的绝对url路径
  • exports:该属性同上文exports模块作用类似。稍有区别就是我们可以通过该属性让exports指向一个新的对象(通常是一个函数)。类似node中改变模块中exports引用。


define(“myModule”,["module"], function(module){
  module.exports = dojo.declare(/*...*/);
});


  原来的exports属性是一个对象,现在是一个类。下次引用该模块时就可以用来实例化对象。

define(["myModule"], function(MyModule){
  var m = new MyModule();
});

  这三个特殊的module是dojo用来兼容其他CommonJs类库时使用。比如将dojo集成进node,这是另一篇文章要将的。
目录
相关文章
Vuex模块module的详解
Vuex 模块是将 store 分割成多个模块的一种方式,每个模块都有自己的状态、mutations、actions 和 getters。这有助于更好地组织和管理应用程序的状态。
|
4月前
nrm 安装后报错 Error [ERR_REQUIRE_ESM]: require() of ES Module
nrm 安装后报错 Error [ERR_REQUIRE_ESM]: require() of ES Module
78 0
|
5月前
|
资源调度
Cannot find module ‘\node_modules\ejs\postinstall.js
Cannot find module ‘\node_modules\ejs\postinstall.js
40 0
|
4月前
【Error】This dependency was not found: * core-js/modules/es.error.cause.js in ./node_modules/@babel
【Error】This dependency was not found: * core-js/modules/es.error.cause.js in ./node_modules/@babel
|
7月前
|
Web App开发 Dart JavaScript
剖析require、import、export、exports、module.exports以及export default 的基本用法
剖析require、import、export、exports、module.exports以及export default 的基本用法
|
设计模式 JavaScript 安全
JS_Module模式深入了解一下
大多数的高级模式都可以互相组合用于构建一个更加方便的模式。如果想要构建一个比较复杂的程序。可以尝试loose augmentation、private state、 和 sub-modules的组合。
153 0
require('module') 是什么?
关于 node 中 module 得解释
62 0
|
JavaScript 开发者
exports和module.exports的区别|学习笔记
快速学习 exports 和 module.exports 的区别
71 0
exports和module.exports的区别|学习笔记
|
JavaScript API
Node.js v13.2.0 开始支持ES modules了
Node.js v13.2.0 开始支持ES modules了
451 0
Node.js v13.2.0 开始支持ES modules了
exports 和 module.exports 的区别
exports 和 module.exports 的区别
111 0

热门文章

最新文章