Giter Club home page Giter Club logo

require.js's Introduction

RequireJS 简单实现

RequireJS 基本实现原理如下

  1. 紧紧盯住require函数中依赖的模块状态,只有当这些依赖模块状态都为loaded则可以进行回调函数的执行
  2. 当然require函数中的依赖模块本身可能也依赖其他模块,那么这就是一个递归的过程,不断下钻去加载依赖模块的依赖模块,直到找到最简单的模块(没有任何依赖的模块),这样看起来就是一个依赖树,最简单模块就是树的叶子节点,那么它的状态必然是loaded
  3. 当每次脚本加载成功之后我们就进行全局检查,检查当前脚本模块的父亲节点的状态是否是loaded,若不是则检查父亲节点的依赖模块是否全部为loaded,若都为loaded那么父亲节点的状态就可以更新为loaded,我们按照此方法递归检查父亲的父亲节点
  4. 检查到最后,直到根节点状态为loaded,那么依赖模块就全部加载完毕了

require.js's People

Watchers

cuizi 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.