Coder Social home page Coder Social logo

node-cycles's Introduction

commonjs循环加载

main.js加载a.js时,然后a.js反过来加载b.js。在这一点上,b.js试图加载a.js。为了防止无限循环,未完成a.js的exports对象的的副本被返回给b.js模块。b.js随后完成加载,其exports对象被提供给a.js模块。

  1. b.js中继续加载a.js时,发现发生了循环加载,就去a.js模块中的exports对象上取值,由于此时a.js模块还没加载完毕,所以只有exports.done = false,这个对象被返回给b.js模块。b.js模块继续执行直到完成,然后a.js模块继续执行直到完成。
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done'); 
  1. main.jsconst b = require('./b.js');加载b模块时,由于b模块已经在const a = require('./a.js');加载a模块的时候加载过了,所以不会小重复加载,会直接取缓存。
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done = %j, b.done = %j', a.done, b.done); 

注意 发生循环加载时,由于模块可能未加载完成,只能返回部分的值,所以引用变量的时候要注意:

const a = require('a.js') // 安全的写法
const foo = require('a.js').foo // 不安全的写法,以为模块a的返回对象可能会有变化,或者foo的值可能会变化。例如官方案例中的exports.done

es6循环加载

执行a.mjs

b.mjs
file:///d:/learn/manual-api/module/cycles/b.mjs:3
console.log(foo);
            ^

ReferenceError: Cannot access 'foo' before initialization
    at file:///d:/learn/manual-api/module/cycles/b.mjs:3:13
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.16.0

es6如何处理循环加载

  1. 执行a.mjs以后,引擎发现它加载了b.mjs,会优先执行b.mjs,然后再执行a.mjs。接着,执行b.mjs的时候,已知它从a.mjs输入了foo接口,这时不会去执行a.mjs,而是认为这个接口已经存在了,继续往下执行。执行到第三行console.log(foo)的时候,才发现这个接口根本没定义,因此报错。
  2. 所以通过将a.mjs模块中的foo改为声明式函数可以解决报错,因为函数可以提升到引用b.mjs之前。

node-cycles's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.