Randomblue
Randomblue

Reputation: 116343

What does it mean to "export to the browser"

The backbone.js annotated source describes the following piece of code

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

as "The top-level namespace. All public Backbone classes and modules will be attached to this. Exported for both CommonJS and the browser."

What does "exported for the browser" mean in this context?

Upvotes: 2

Views: 190

Answers (1)

Brian Genisio
Brian Genisio

Reputation: 48147

In CommonJS, your modules are sequestered and anything you want to share with the thing that requires you is shared through the "exports" variable. Node.js, for instance, uses this.

On the other hand, if you are just in the browser, then you don't use the exports variable and you add a new variable in root which ultimately points to the window global var.

In other words, if we are using something that supports CommonJS, export Backbone. If not, put it in the root context instead.

Upvotes: 1

Related Questions