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" + ); +} )(); +