]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): use correct thisArg for collection method callbacks (#1132)
author龙腾道 <LongTengDao@LongTengDao.com>
Wed, 6 May 2020 17:41:34 +0000 (01:41 +0800)
committerGitHub <noreply@github.com>
Wed, 6 May 2020 17:41:34 +0000 (13:41 -0400)
packages/reactivity/__tests__/collections/Set.spec.ts
packages/reactivity/src/collectionHandlers.ts

index bf26e807dd564a574522f4ef6e3ade7733b5bc7b..cdccd347999df3a771b6f0d508bfe6a5c1eb26c0 100644 (file)
@@ -412,5 +412,19 @@ describe('reactivity/collections', () => {
         `Reactive Set contains both the raw and reactive`
       ).toHaveBeenWarned()
     })
+    
+    it('thisArg', () => {
+      const raw = new Set([ 'value' ])
+      const proxy = reactive(raw)
+      const thisArg = {}
+      let count = 0
+      proxy.forEach(function (this :{}, value, _, set) {
+        ++count
+        expect(this).toBe(thisArg)
+        expect(value).toBe('value')
+        expect(set).toBe(proxy)
+      }, thisArg)
+      expect(count).toBe(1)
+    })
   })
 })
index b03cbd589ae2249f82136a965cdf89366cb5d022..5aff152e939d7e6edf0ae74669f67c5037ee9efd 100644 (file)
@@ -146,9 +146,9 @@ function createForEach(isReadonly: boolean) {
     // 1. invoked with the reactive map as `this` and 3rd arg
     // 2. the value received should be a corresponding reactive/readonly.
     function wrappedCallback(value: unknown, key: unknown) {
-      return callback.call(observed, wrap(value), wrap(key), observed)
+      return callback.call(thisArg, wrap(value), wrap(key), observed)
     }
-    return getProto(target).forEach.call(target, wrappedCallback, thisArg)
+    return getProto(target).forEach.call(target, wrappedCallback)
   }
 }