Skip to content

Commit e08f6f0

Browse files
authored
fix(reactivity): use correct thisArg for collection method callbacks (#1132)
1 parent 4df1806 commit e08f6f0

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/reactivity/__tests__/collections/Set.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -412,5 +412,19 @@ describe('reactivity/collections', () => {
412412
`Reactive Set contains both the raw and reactive`
413413
).toHaveBeenWarned()
414414
})
415+
416+
it('thisArg', () => {
417+
const raw = new Set([ 'value' ])
418+
const proxy = reactive(raw)
419+
const thisArg = {}
420+
let count = 0
421+
proxy.forEach(function (this :{}, value, _, set) {
422+
++count
423+
expect(this).toBe(thisArg)
424+
expect(value).toBe('value')
425+
expect(set).toBe(proxy)
426+
}, thisArg)
427+
expect(count).toBe(1)
428+
})
415429
})
416430
})

packages/reactivity/src/collectionHandlers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ function createForEach(isReadonly: boolean) {
146146
// 1. invoked with the reactive map as `this` and 3rd arg
147147
// 2. the value received should be a corresponding reactive/readonly.
148148
function wrappedCallback(value: unknown, key: unknown) {
149-
return callback.call(observed, wrap(value), wrap(key), observed)
149+
return callback.call(thisArg, wrap(value), wrap(key), observed)
150150
}
151-
return getProto(target).forEach.call(target, wrappedCallback, thisArg)
151+
return getProto(target).forEach.call(target, wrappedCallback)
152152
}
153153
}
154154

0 commit comments

Comments
 (0)