Skip to content

Commit 1612971

Browse files
authored
fix(compat): copy additional properties for functions bound via globalProperties (#4873)
close #4403
1 parent c6eb3cc commit 1612971

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/runtime-core/src/componentPublicInstance.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
356356
return desc.get.call(instance.proxy)
357357
} else {
358358
const val = globalProperties[key]
359-
return isFunction(val) ? val.bind(instance.proxy) : val
359+
return isFunction(val)
360+
? Object.assign(val.bind(instance.proxy), val)
361+
: val
360362
}
361363
} else {
362364
return globalProperties[key]

packages/vue-compat/__tests__/global.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,28 @@ describe('GLOBAL_PROTOTYPE', () => {
285285
delete Vue.prototype.$test
286286
})
287287

288+
test.only('functions keeps additional properties', () => {
289+
function test(this: any) {
290+
return this.msg
291+
}
292+
test.additionalFn = () => {
293+
return 'additional fn'
294+
}
295+
296+
Vue.prototype.$test = test
297+
const vm = new Vue({
298+
data() {
299+
return {
300+
msg: 'test'
301+
}
302+
}
303+
}) as any
304+
expect(typeof vm.$test).toBe('function')
305+
expect(typeof vm.$test.additionalFn).toBe('function')
306+
expect(vm.$test.additionalFn()).toBe('additional fn')
307+
delete Vue.prototype.$test
308+
})
309+
288310
test('extended prototype', async () => {
289311
const Foo = Vue.extend()
290312
Foo.prototype.$test = 1

0 commit comments

Comments
 (0)