1
0
Fork 0

util.clone() no longer falsely attempts to clone functions

closure/master
Mike Gerwitz 2011-04-05 23:47:08 -04:00
parent 4a0537223b
commit 604e03fa55
2 changed files with 23 additions and 0 deletions

View File

@ -146,6 +146,12 @@ exports.clone = function clone( data, deep )
return ret; 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 ) else if ( data instanceof Object )
{ {
var newobj = {}, var newobj = {},

View File

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