requirejs - what export exactly do here?

旧巷老猫 提交于 2019-12-08 01:46:29

问题


I use the require.js - when i use the requiredjs, i am not get the underscore and backbone while i console without using shim's export modoule.

But jquery not asking this export shim dependent.. so, why we need to use the shim and it's export for the underscore and backbone?

here is my code:

requirejs.config({
    baseUrl: 'js',
    paths: {
        "jquery":'lib/jquery-1.9.1.min',
        "underscore":"lib/underscore-min",
        "backbone" : "lib/backbone-min"
    },
    shim:{
        "underscore":{
            exports: '_' 
                   //what is does here? without this i am getting undefined
        },
        "backbone":{
            exports: 'Backbone' 
                    //what is does here? without this i am getting undefined
        }
    }
});

    require(["jquery","underscore","backbone"],function ($,_,Backbone) {
        console.log($,_,Backbone);
//without shim export i am getting conosle like this:
// "function(), undefined, udefined" - why?
    });

回答1:


Backbone and underscore aren't AMD-compliant, they store themselves in global scope (i.e. in the window element in a browser environment). shim element allows exposing their global variables like if they were AMD modules, by "linking" the global variable (_ in case of underscore and Backbone in case of Backbone) with the "exports" part of the "virtual" module (I called it "virtual" because this happens on the fly, you don't have to change any code).

This:

"underscore":{
    exports: '_' 
}

means that adding dependency on "underscore" will grab a reference to window._ and expose it as an AMD module.


jQuery doesn't need it because it detects whether it's loaded as an AMD module and exposes itself in an AMD-compliant way in that case (scroll down to the very bottom of the original source code for more details)



来源:https://stackoverflow.com/questions/16401704/requirejs-what-export-exactly-do-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!