From 3c54f7911339261f0e54dc3451d8ae981ca42df9 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 24 Oct 2011 13:19:15 -0400 Subject: [PATCH] fixes #347 --- test/objects.js | 4 ++++ underscore.js | 1 + 2 files changed, 5 insertions(+) diff --git a/test/objects.js b/test/objects.js index 967ba6b21..7e6cd7cc1 100644 --- a/test/objects.js +++ b/test/objects.js @@ -66,6 +66,10 @@ $(document).ready(function() { clone.lucky.push(101); equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original'); + + equals(_.clone(undefined), void 0, 'non objects should not be changed by clone'); + equals(_.clone(1), 1, 'non objects should not be changed by clone'); + equals(_.clone(null), null, 'non objects should not be changed by clone'); }); test("objects: isEqual", function() { diff --git a/underscore.js b/underscore.js index 04f3311cd..5470fa6b3 100644 --- a/underscore.js +++ b/underscore.js @@ -645,6 +645,7 @@ // Create a (shallow-cloned) duplicate of an object. _.clone = function(obj) { + if (!_.isObject(obj)) return obj; return _.isArray(obj) ? obj.slice() : _.extend({}, obj); };