* Those data types can be cloned: * Plain object, Array, TypedArray, number, string, null, undefined. * Those data types will be assgined using the orginal data: * BUILTIN_OBJECT * Instance of user defined class will be cloned to a plain object, without * properties in prototype. * Oth
(source)
| 307 | * @return {*} new |
| 308 | */ |
| 309 | function clone(source) { |
| 310 | if (source == null || typeof source !== 'object') { |
| 311 | return source; |
| 312 | } |
| 313 | |
| 314 | var result = source; |
| 315 | var typeStr = objToString.call(source); |
| 316 | |
| 317 | if (typeStr === '[object Array]') { |
| 318 | if (!isPrimitive(source)) { |
| 319 | result = []; |
| 320 | for (var i = 0, len = source.length; i < len; i++) { |
| 321 | result[i] = clone(source[i]); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | else if (TYPED_ARRAY[typeStr]) { |
| 326 | if (!isPrimitive(source)) { |
| 327 | var Ctor = source.constructor; |
| 328 | if (source.constructor.from) { |
| 329 | result = Ctor.from(source); |
| 330 | } |
| 331 | else { |
| 332 | result = new Ctor(source.length); |
| 333 | for (var i = 0, len = source.length; i < len; i++) { |
| 334 | result[i] = clone(source[i]); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | else if (!BUILTIN_OBJECT[typeStr] && !isPrimitive(source) && !isDom(source)) { |
| 340 | result = {}; |
| 341 | for (var key in source) { |
| 342 | if (source.hasOwnProperty(key)) { |
| 343 | result[key] = clone(source[key]); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * @memberOf module:zrender/core/util |
no test coverage detected