11var clone = ( function ( ) {
22'use strict' ;
33
4+ function _instanceof ( obj , type ) {
5+ return type != null && obj instanceof type ;
6+ }
7+
48var nativeMap ;
59try {
610 nativeMap = Map ;
@@ -80,11 +84,11 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
8084 return parent ;
8185 }
8286
83- if ( parent instanceof nativeMap ) {
87+ if ( _instanceof ( parent , nativeMap ) ) {
8488 child = new nativeMap ( ) ;
85- } else if ( parent instanceof nativeSet ) {
89+ } else if ( _instanceof ( parent , nativeSet ) ) {
8690 child = new nativeSet ( ) ;
87- } else if ( parent instanceof nativePromise ) {
91+ } else if ( _instanceof ( parent , nativePromise ) ) {
8892 child = new nativePromise ( function ( resolve , reject ) {
8993 parent . then ( function ( value ) {
9094 resolve ( _clone ( value , depth - 1 ) ) ;
@@ -103,7 +107,7 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
103107 child = new Buffer ( parent . length ) ;
104108 parent . copy ( child ) ;
105109 return child ;
106- } else if ( parent instanceof Error ) {
110+ } else if ( _instanceof ( parent , Error ) ) {
107111 child = Object . create ( parent ) ;
108112 } else {
109113 if ( typeof prototype == 'undefined' ) {
@@ -126,28 +130,18 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
126130 allChildren . push ( child ) ;
127131 }
128132
129- if ( parent instanceof nativeMap ) {
130- var keyIterator = parent . keys ( ) ;
131- while ( true ) {
132- var next = keyIterator . next ( ) ;
133- if ( next . done ) {
134- break ;
135- }
136- var keyChild = _clone ( next . value , depth - 1 ) ;
137- var valueChild = _clone ( parent . get ( next . value ) , depth - 1 ) ;
133+ if ( _instanceof ( parent , nativeMap ) ) {
134+ parent . forEach ( function ( value , key ) {
135+ var keyChild = _clone ( key , depth - 1 ) ;
136+ var valueChild = _clone ( value , depth - 1 ) ;
138137 child . set ( keyChild , valueChild ) ;
139- }
138+ } ) ;
140139 }
141- if ( parent instanceof nativeSet ) {
142- var iterator = parent . keys ( ) ;
143- while ( true ) {
144- var next = iterator . next ( ) ;
145- if ( next . done ) {
146- break ;
147- }
148- var entryChild = _clone ( next . value , depth - 1 ) ;
140+ if ( _instanceof ( parent , nativeSet ) ) {
141+ parent . forEach ( function ( value ) {
142+ var entryChild = _clone ( value , depth - 1 ) ;
149143 child . add ( entryChild ) ;
150- }
144+ } ) ;
151145 }
152146
153147 for ( var i in parent ) {
0 commit comments