elaborate underscore initialization so that it works seamlessly on CommonJS, as well as in the browser

This commit is contained in:
Jeremy Ashkenas
2009-10-28 23:53:40 -04:00
parent 4f783846de
commit a82a01ebfc
2 changed files with 22 additions and 12 deletions

View File

@@ -12,9 +12,13 @@ $(document).ready(function() {
equals(answer, 2, 'the loop broke in the middle'); equals(answer, 2, 'the loop broke in the middle');
var answers = []; var answers = [];
_.each([1, 2, 3], function(num) { answers.push(num * this.multiplier);}, {multiplier : 5}); _.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5});
equals(answers.join(', '), '5, 10, 15', 'context object property accessed'); equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
answers = [];
_.each("moe", function(letter){ answers.push(letter); });
equals(answers.join(', '), 'm, o, e', 'iterates over the letters in strings');
answers = []; answers = [];
_.forEach([1, 2, 3], function(num){ answers.push(num); }); _.forEach([1, 2, 3], function(num){ answers.push(num); });
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"'); equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');

View File

@@ -8,15 +8,25 @@
(function() { (function() {
var root = (typeof window != 'undefined') ? window : exports; /*------------------------- Baseline setup ---------------------------------*/
var previousUnderscore = root._; // Are we running in CommonJS or in the browser?
var commonJS = (typeof window === 'undefined' && typeof exports !== 'undefined');
// Save the previous value of the "_" variable.
var previousUnderscore = commonJS ? null : window._;
// Keep the identity function around for default iterators.
var identity = function(value) { return value; }; var identity = function(value) { return value; };
var _ = root._ = {};
_.VERSION = '0.2.0'; // Create a safe reference to the Underscore object for the functions below.
var _ = {};
// Export the Underscore object for CommonJS, assign it globally otherwise.
commonJS ? _ = exports : window._ = _;
// Current version.
_.VERSION = '0.2.1';
/*------------------------ Collection Functions: ---------------------------*/ /*------------------------ Collection Functions: ---------------------------*/
@@ -417,7 +427,7 @@
// Run Underscore.js in noConflict mode, returning the '_' variable to its // Run Underscore.js in noConflict mode, returning the '_' variable to its
// previous owner. Returns a reference to the Underscore object. // previous owner. Returns a reference to the Underscore object.
_.noConflict = function() { _.noConflict = function() {
root._ = previousUnderscore; if (!commonJS) window._ = previousUnderscore;
return this; return this;
}; };
@@ -452,10 +462,6 @@
_.inject = _.reduce; _.inject = _.reduce;
_.filter = _.select; _.filter = _.select;
_.every = _.all; _.every = _.all;
_.some = _.any; _.some = _.any;
/*------------------------- Export for ServerJS ----------------------------*/
if (typeof exports != 'undefined') exports = _;
})(); })();