repl.txt (1926B)
1 2 {{alias}}( value[, level] ) 3 Copy or deep clone a value to an arbitrary depth. 4 5 The implementation can handle circular references. 6 7 If a `Number`, `String`, or `Boolean` object is encountered, the value is 8 cloned as a primitive. This behavior is intentional. 9 10 For objects, the implementation only copies enumerable keys and their 11 associated property descriptors. 12 13 The implementation only checks whether basic `Objects`, `Arrays`, and class 14 instances are extensible, sealed, and/or frozen. 15 16 Functions are not cloned; their reference is copied. 17 18 The implementation supports custom error types which are `Error` instances 19 (e.g., ES2015 subclasses). 20 21 Support for copying class instances is inherently fragile. Any instances 22 with privileged access to variables (e.g., within closures) cannot be 23 cloned. This stated, basic copying of class instances is supported. Provided 24 an environment which supports ES5, the implementation is greedy and performs 25 a deep clone of any arbitrary class instance and its properties. The 26 implementation assumes that the concept of `level` applies only to the class 27 instance reference, but not to its internal state. 28 29 Parameters 30 ---------- 31 value: any 32 Value to test. 33 34 level: integer (optional) 35 Copy depth. Default: Infinity. 36 37 Returns 38 ------- 39 out: any 40 Value copy. 41 42 Examples 43 -------- 44 > var value = [ { 'a': 1, 'b': true, 'c': [ 1, 2, 3 ] } ]; 45 > var out = {{alias}}( value ) 46 [ { 'a': 1, 'b': true, 'c': [ 1, 2, 3 ] } ] 47 > var bool = ( value[ 0 ].c === out[ 0 ].c ) 48 false 49 50 // Set the `level` option to limit the copy depth: 51 > value = [ { 'a': 1, 'b': true, 'c': [ 1, 2, 3 ] } ]; 52 > out = {{alias}}( value, 1 ); 53 > bool = ( value[ 0 ] === out[ 0 ] ) 54 true 55 > bool = ( value[ 0 ].c === out[ 0 ].c ) 56 true 57 58 59 See Also 60 -------- 61