simple example for using require.js

后端 未结 3 636
广开言路
广开言路 2021-01-31 04:44

I am trying to learn how to use require.js. So I made an HTML page with the following tags in the body.



        
3条回答
  •  星月不相逢
    2021-01-31 05:25

    In addition to Joseph's answer, you can also write other modules that depend on shirt (which is where the real power of RequireJS comes in).

    // shirt.js
    define({
        color: "black",
        size : "large"
    });
    
    // logger.js
    define(function (require) {
        var shirt = require("./shirt");
    
        return {
            logTheShirt: function () {
                console.log("color: " + shirt.color + ", size: " + shirt.size);
            }
        };
    });
    
    // main.js
    define(function (require) {
        var shirt = require("./shirt");
        var logger = require("./logger");
    
        alert("Shirt color is: " + shirt.color);
        logger.logTheShirt();
    });
    

    Then your HTML can just be

    
    

提交回复
热议问题