1
0
Fork 0

util.clone() primitive fix (broken in recent commit)

- null is considered to be type "object" by instanceof
perfodd
Mike Gerwitz 2011-12-22 09:37:34 -05:00
parent 021b67bbff
commit 6295b83ec7
2 changed files with 27 additions and 1 deletions

View File

@ -150,7 +150,9 @@ exports.clone = function clone( data, deep )
// support toSource(), they'd still do the same damn thing.
return data;
}
else if ( typeof data === 'object' )
// explicitly testing with instanceof will ensure we're actually testing an
// object, not something that may be misinterpreted as one (e.g. null)
else if ( data instanceof Object )
{
var newobj = {},
hasOwn = Object.prototype.hasOwnProperty;

View File

@ -119,3 +119,27 @@ for ( var prop in deep_obj )
);
} )();
/**
* Primitives cannot be cloned, so we should expect that they are simply
* returned
*/
( function testPrimitivesAreProperlyReturnedByClone()
{
// we don't try NaN because NaN != NaN; we'll try it separately
var prim = [ null, 1, true, false, undefined ],
i = prim.length;
while ( i-- )
{
var val = prim[ i ];
assert.equal( val, util.clone( val ),
'Failed to clone primitive value: ' + val
);
}
// test NaN separately
assert.ok( isNaN( util.clone( NaN ) ) );
} )();