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 = {};
}
Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.
Module pattern. The Module pattern is used to mimic the concept of classes (since JavaScript doesn't natively support classes) so that we can store both public and private methods and variables inside a single object — similar to how classes are used in other programming languages like Java or Python.
By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.
Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With