Reputation:
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');
}
Upvotes: 6
Views: 5139
Reputation: 9569
That's there to allow Backbone.js
to be used as a Common.js
module I believe. More details here: http://wiki.commonjs.org/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.
Upvotes: 4