Giter Site home page Giter Site logo

fake-requirejs's Introduction

项目说明

通过模仿requirejs,学习如何写一个模块加载器。 用浏览器打开example/index.html可以直接查看demo

使用方法

引入fake-require.js文件

<script src="../require.js" data-main="./main.js"></script>

data-main属性为指定的入口js文件。当_fake-require.js加载完之后,就会马上请求该入口文件。

编写入口文件main.js

// main.js
require(['a', 'b'], function (a, b) {
    a.hi();
    b.goodbye();
}, function () {
    console.error('Something wrong with the dependent modules.');
});

require函数接受三个参数。

  1. 第一个为依赖的模块数组
  2. 第二个为依赖模块数组都加载成功后的回调函数,其中参数a,b对应相应的模块
  3. 第三个为失败回调函数,任意一个依赖的模块失败都会触发此函数。

编写模块

//a.js
define(function () {
    var hi = function () {
        console.log('hi');
    };

    return {
        hi: hi
    }
});
// b.js
define(function () {
    var goodbye = function () {
        console.log('goodbye');
    };

    return {
        goodbye: goodbye
    }
});

fake-requirejs's People

Contributors

youngwind avatar

Watchers

 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.