diff --git a/src/v3/reactivity/readonly.ts b/src/v3/reactivity/readonly.ts index 19179e02b..933f910b6 100644 --- a/src/v3/reactivity/readonly.ts +++ b/src/v3/reactivity/readonly.ts @@ -69,7 +69,7 @@ function createReadonly(target: any, shallow: boolean) { return existingProxy } - const proxy = {} + const proxy = Object.create(Object.getPrototypeOf(target)) def(target, existingFlag, proxy) def(proxy, ReactiveFlags.IS_READONLY, true) diff --git a/test/unit/features/v3/reactivity/readonly.spec.ts b/test/unit/features/v3/reactivity/readonly.spec.ts index ab4c52f7c..11550ab56 100644 --- a/test/unit/features/v3/reactivity/readonly.spec.ts +++ b/test/unit/features/v3/reactivity/readonly.spec.ts @@ -499,4 +499,25 @@ describe('reactivity/readonly', () => { expect(obj.ror).toBe(true) expect(toRaw(obj).ror).not.toBe(ror) // ref successfully replaced }) + + test('compatiblity with classes', () => { + const spy = vi.fn() + class Foo { + x = 1 + log() { + spy(this.x) + } + change() { + this.x++ + } + } + const foo = new Foo() + const readonlyFoo = readonly(foo) + readonlyFoo.log() + expect(spy).toHaveBeenCalledWith(1) + + readonlyFoo.change() + expect(readonlyFoo.x).toBe(1) + expect(`et operation on key "x" failed`).toHaveBeenWarned() + }) })