Skip to content

Commit

Permalink
fix(reactivity): use correct thisArg for collection method callbacks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LongTengDao authored May 6, 2020
1 parent 4df1806 commit e08f6f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/reactivity/__tests__/collections/Set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
4 changes: 2 additions & 2 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit e08f6f0

Please sign in to comment.