javascript模块导出模式 [英] javascript module exports pattern

查看:95
本文介绍了javascript模块导出模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下导出变量的内容:



从backbone.js复制,我也注意到spine.js使用相同的模式。



https://gist.github.com/1375748

  var Backbone; 
if(typeof exports!=='undefined'){
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}


解决方案

该模块模式是 CommonJS 规范称为 CommonJS模块


在一个模块中,有一个名为exports的自由变量,这是一个
对象,模块可以在执行时添加其API。


所以基本上添加到导出对象定义您的模块暴露的API。


can someone please explain what's the exports variable for:

copied from backbone.js, I also noticed spine.js uses the same pattern.

https://gist.github.com/1375748

var Backbone;
if (typeof exports !== 'undefined') {
    Backbone = exports;
} else {
    Backbone = root.Backbone = {};
}

解决方案

That module pattern is part of the CommonJS specification called CommonJS Modules:

In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.

So basically adding to the exports object defines the API your module exposes.

这篇关于javascript模块导出模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆