From: daiwei Date: Fri, 3 Jan 2025 08:25:10 +0000 (+0800) Subject: feat(reactivity): collection iteration should inherit iterator instance methods X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F12644%2Fhead;p=thirdparty%2Fvuejs%2Fcore.git feat(reactivity): collection iteration should inherit iterator instance methods --- diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index 048b7f3886..94c970f236 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -55,22 +55,26 @@ function createIterableMethod( ) // return a wrapped iterator which returns observed versions of the // values emitted from the real iterator - return { - // iterator protocol - next() { - const { value, done } = innerIterator.next() - return done - ? { value, done } - : { - value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value), - done, - } - }, - // iterable protocol - [Symbol.iterator]() { - return this + return extend( + // inheriting all iterator properties + Object.create(innerIterator), + { + // iterator protocol + next() { + const { value, done } = innerIterator.next() + return done + ? { value, done } + : { + value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value), + done, + } + }, + // iterable protocol + [Symbol.iterator]() { + return this + }, }, - } + ) } }