fix(reactivity): use correct thisArg for collection method callbacks (#1132)
This commit is contained in:
parent
4df180607d
commit
e08f6f0ede
@ -412,5 +412,19 @@ describe('reactivity/collections', () => {
|
|||||||
`Reactive Set contains both the raw and reactive`
|
`Reactive Set contains both the raw and reactive`
|
||||||
).toHaveBeenWarned()
|
).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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -146,9 +146,9 @@ function createForEach(isReadonly: boolean) {
|
|||||||
// 1. invoked with the reactive map as `this` and 3rd arg
|
// 1. invoked with the reactive map as `this` and 3rd arg
|
||||||
// 2. the value received should be a corresponding reactive/readonly.
|
// 2. the value received should be a corresponding reactive/readonly.
|
||||||
function wrappedCallback(value: unknown, key: unknown) {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user