source: trunk/termtv/js/clone.js@ 17408

Last change on this file since 17408 was 17407, checked in by tbretz, 12 years ago
First version.
File size: 658 bytes
Line 
1/**
2 * @public
3 *
4 * This clones all objects and arrays of the given element recursively.
5 * Functions are not cloned. This is sometimes necessary to fork
6 * the usage of information, beause otherwise javascript only passes
7 * references.
8 *
9 * @param {Object} src
10 * Source object to be cloned
11 *
12 * @returns {Object}
13 * The clone of the src object
14 */
15function clone(src)
16{
17 if (typeof(src) != 'object' || src===null)
18 return src;
19
20 var copy = typeof(src.length)!='undefined' ? new Array(src.length) : new Object();
21 for (var key in src)
22 copy[key] = typeof(src[key])=='object' ? clone(src[key]) : src[key];
23 return copy;
24}
Note: See TracBrowser for help on using the repository browser.