admin管理员组文章数量:1426338
In the first few lines of backbone.js, I don't see why they are testing for undefined on exports or require
It seems obvious that it would be undefined as they did not set it. If it was a global(window) object then they would have said it explicitly.
root.exports // they don't do this
root.require
Why do they check this?
typeof exports !== 'undefined'
and this
if (!_ && (typeof require !== 'undefined'))
and this from above
!_
Full Snippet
(function(){
var root = this,
previousBackbone = root.Backbone,
slice = Array.prototype.slice,
splice = Array.prototype.splice,
_ = root._,
Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
Backbone.VERSION = '0.9.2';
if (!_ && (typeof require !== 'undefined')) {
_ = require('underscore');
}
In the first few lines of backbone.js, I don't see why they are testing for undefined on exports or require
It seems obvious that it would be undefined as they did not set it. If it was a global(window) object then they would have said it explicitly.
root.exports // they don't do this
root.require
Why do they check this?
typeof exports !== 'undefined'
and this
if (!_ && (typeof require !== 'undefined'))
and this from above
!_
Full Snippet
(function(){
var root = this,
previousBackbone = root.Backbone,
slice = Array.prototype.slice,
splice = Array.prototype.splice,
_ = root._,
Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
Backbone.VERSION = '0.9.2';
if (!_ && (typeof require !== 'undefined')) {
_ = require('underscore');
}
Share
Improve this question
edited Sep 27, 2012 at 13:56
asked Sep 27, 2012 at 13:45
user656925user656925
5
- exports is a node.js global variable for exposing public methods in the creation of modules. nodejs/docs/latest/api/modules.html . Backbone.js appears to be one of these modules. – killercowuk Commented Sep 27, 2012 at 13:51
- o.k...why didn't they use root.exports...this is an optimization to shorten the lookup chain. – user656925 Commented Sep 27, 2012 at 13:52
- I think that this has been answered previously: stackoverflow./questions/8178535/… – Daniel Aranda Commented Sep 27, 2012 at 13:53
- no it hasn't exactly...the issue is that they don't user root...to municate to the reader that this is a global. – user656925 Commented Sep 27, 2012 at 13:53
- Maybe because "root" is not defined in browsers global "window" and would throw, as this check is an environment check – René Baudisch Commented Jun 2, 2021 at 17:58
1 Answer
Reset to default 4That's there to allow Backbone.js
to be used as a Common.js
module I believe. More details here: http://wiki.monjs/wiki/Modules/1.1
Specifically this bit:
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.
Also, this bit covers your question about require
:
In a module, there is a free variable "require", that is a Function. The "require" function accepts a module identifier. "require" returns the exported API of the foreign module.
本文标签: javascriptWhat is exports object forStack Overflow
版权声明:本文标题:javascript - What is exports object for? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449389a2658813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论