From 4f783846de50e8de6d3001eb1ae23b9d041ccf23 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
Underscore is a
- utility-belt library for Javascript that provides a lot of the
+ utility-belt library for JavaScript that provides a lot of the
functional programming support that you would expect in
Prototype.js
(or Ruby),
- but without extending any of the built-in Javascript objects. It's the
+ but without extending any of the built-in JavaScript objects. It's the
tie to go along with jQuery's tux.
Iterates over a list of elements, yielding each in turn to an iterator
function. The iterator is bound to the context object, if one is
- passed. If list is a Javascript object, a pair with key
+ passed. If list is a JavaScript object, a pair with key
and value properties will be yielded. If the list has an each
method of its own, it will be used instead. Delegates to the native
forEach function if it exists.
@@ -403,7 +403,7 @@ _.last([3, 2, 1]);
compact_.compact(array)
Returns a copy of the array with all falsy values removed.
- In Javascript, false, null, 0, "",
+ In JavaScript, false, null, 0, "",
undefined and NaN are all falsy.
@@ -713,10 +713,10 @@ _.uniqueId('contact_');
template_.template(templateString, [context])
- Compiles Javascript templates into functions that can be evaluated
+ Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON
data sources. Template functions can both interpolate variables, using
- <%= … %>, as well as execute arbitrary Javascript code, with
+ <%= … %>, as well as execute arbitrary JavaScript code, with
<% … %>. When you evaluate a template function, pass in a
context object that has properties corresponding to the template's free
variables. If you're writing a one-off, you can pass the context
diff --git a/package.json b/package.json
index 3e7ce81f3..2cbc8128d 100644
--- a/package.json
+++ b/package.json
@@ -1,12 +1,11 @@
{
- "!": "This is a Narwhal package descriptor.",
- "name": "underscore",
- "description": "Functional programming aid for Javascript. Works well with jQuery.",
- "url": "http://documentcloud.github.com/underscore/",
- "keywords": ["util", "functional", "server", "client", "browser"],
- "author": "Jeremy Ashkenas ",
- "maintainer": "Kris Kowal ",
- "contributors": [],
- "dependencies": [],
- "lib": "."
+ "name" : "underscore",
+ "description" : "Functional programming aid for JavaScript. Works well with jQuery.",
+ "url" : "http://documentcloud.github.com/underscore/",
+ "keywords" : ["util", "functional", "server", "client", "browser"],
+ "author" : "Jeremy Ashkenas ",
+ "maintainer" : "Kris Kowal ",
+ "contributors" : [],
+ "dependencies" : [],
+ "lib" : "."
}
diff --git a/test/collections.js b/test/collections.js
index f97e6e520..8fb684a07 100644
--- a/test/collections.js
+++ b/test/collections.js
@@ -18,6 +18,13 @@ $(document).ready(function() {
answers = [];
_.forEach([1, 2, 3], function(num){ answers.push(num); });
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
+
+ answers = [];
+ var obj = {one : 1, two : 2, three : 3};
+ obj.constructor.prototype.four = 4;
+ _.each(obj, function(pair){ answers.push(pair.key); });
+ equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
+ delete obj.constructor.prototype.four;
});
test('collections: map', function() {
diff --git a/underscore.js b/underscore.js
index c89842dd9..f5b8610c2 100644
--- a/underscore.js
+++ b/underscore.js
@@ -12,6 +12,8 @@
var previousUnderscore = root._;
+ var identity = function(value) { return value; };
+
var _ = root._ = {};
_.VERSION = '0.2.0';
@@ -26,7 +28,7 @@
if (obj.forEach) {
obj.forEach(iterator, context);
} else if (obj.length) {
- for (var i=0, ii = obj.length; i> 1;
@@ -273,7 +276,7 @@
// item in an array, or -1 if the item is not included in the array.
_.indexOf = function(array, item) {
if (array.indexOf) return array.indexOf(item);
- for (i=0, ii=array.length; i