requirejs - 这里出口到底是什么?

时间:2013-05-06 15:10:13

标签: requirejs

我使用的是require.js - 当我使用requiredjs时,我没有得到下划线和骨干,而我没有使用shim的导出模块。

但是jquery没有要求这个出口垫片依赖..那么,为什么我们需要使用垫片并将其导出为下划线和骨干?

这是我的代码:

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 个答案:

答案 0 :(得分:4)

Backboneunderscore不符合AMD标准,它们将自己存储在全局范围内(即在浏览器环境中的window元素中)。 shim元素允许通过“链接”全局变量(在下划线的情况下_和在Backbone的情况下为Backbone)与“导出”来公开它们的全局变量(如果它们是AMD模块) “虚拟”模块的一部分(我将其称为“虚拟”,因为这是在运行中发生的,您不必更改任何代码)。

此:

"underscore":{
    exports: '_' 
}

表示添加对“下划线”的依赖将获取对window._的引用并将其作为AMD模块公开。


jQuery不需要它,因为它检测它是否作为AMD模块加载并在这种情况下以符合AMD的方式暴露自身(向下滚动到original source code的最底部更多细节)

相关问题