From 604e03fa554674616d72b387a6de535c2f4b3a94 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 5 Apr 2011 23:47:08 -0400 Subject: [PATCH] util.clone() no longer falsely attempts to clone functions --- lib/util.js | 6 ++++++ test/test-util-clone.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/util.js b/lib/util.js index 148674e..c716c9d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -146,6 +146,12 @@ exports.clone = function clone( data, deep ) return ret; } + else if ( typeof data === 'function' ) + { + // It is pointless to clone a function. Even if we did clone those that + // support toSource(), they'd still do the same damn thing. + return data; + } else if ( data instanceof Object ) { var newobj = {}, diff --git a/test/test-util-clone.js b/test/test-util-clone.js index 4305618..cf9cdb2 100644 --- a/test/test-util-clone.js +++ b/test/test-util-clone.js @@ -103,3 +103,20 @@ for ( prop in deep_obj ) ); } + +/** + * "Cloning" functions doesn't necessarily make sense. It can, depending on how + * you think about it. We can do a toSource() in many circumstances and create a + * new function from that. But what's the point? It still does the same thing. + * As such, functions will not be cloned. They'll be returned by reference. + */ +( function testCloneDoesNothingWithFunctions() +{ + var func = function() {}, + obj = { foo: func }; + + assert.ok( func === util.clone( obj, true ).foo, + "Functions should not be cloned" + ); +} )(); +