From 16129714714e19c5c6bfbd05c439ff68bcac00b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20L=C3=BCnborg?= Date: Wed, 13 Apr 2022 11:53:07 +0200 Subject: [PATCH] fix(compat): copy additional properties for functions bound via globalProperties (#4873) close #4403 --- .../src/componentPublicInstance.ts | 4 +++- packages/vue-compat/__tests__/global.spec.ts | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index c2d36e77..018187fc 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -356,7 +356,9 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { return desc.get.call(instance.proxy) } else { const val = globalProperties[key] - return isFunction(val) ? val.bind(instance.proxy) : val + return isFunction(val) + ? Object.assign(val.bind(instance.proxy), val) + : val } } else { return globalProperties[key] diff --git a/packages/vue-compat/__tests__/global.spec.ts b/packages/vue-compat/__tests__/global.spec.ts index 86bb4391..06109a67 100644 --- a/packages/vue-compat/__tests__/global.spec.ts +++ b/packages/vue-compat/__tests__/global.spec.ts @@ -285,6 +285,28 @@ describe('GLOBAL_PROTOTYPE', () => { delete Vue.prototype.$test }) + test.only('functions keeps additional properties', () => { + function test(this: any) { + return this.msg + } + test.additionalFn = () => { + return 'additional fn' + } + + Vue.prototype.$test = test + const vm = new Vue({ + data() { + return { + msg: 'test' + } + } + }) as any + expect(typeof vm.$test).toBe('function') + expect(typeof vm.$test.additionalFn).toBe('function') + expect(vm.$test.additionalFn()).toBe('additional fn') + delete Vue.prototype.$test + }) + test('extended prototype', async () => { const Foo = Vue.extend() Foo.prototype.$test = 1