Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compat): don't bind function from globalProperties to the current istance if it has additional properties #4873

Merged
merged 3 commits into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/runtime-core/src/componentPublicInstance.ts
Expand Up @@ -356,7 +356,9 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
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]
Expand Down
22 changes: 22 additions & 0 deletions packages/vue-compat/__tests__/global.spec.ts
Expand Up @@ -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
Expand Down