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
Add a ment  | 

1 Answer 1

Reset to default 4

That'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