diff --git a/packages/reactive/src/__tests__/observable.spec.ts b/packages/reactive/src/__tests__/observable.spec.ts index 3d6910f4443..56c941aea6c 100644 --- a/packages/reactive/src/__tests__/observable.spec.ts +++ b/packages/reactive/src/__tests__/observable.spec.ts @@ -31,3 +31,17 @@ test('observable contains', () => { expect(contains(obj, obj.arr)).toBe(true) expect(contains(obj, arr)).toBe(true) }) + +test('observable __proto__', () => { + const observableArr = observable([] as any[]) + // @ts-ignore + observableArr.__proto__ = Object.create(Array.prototype) + observableArr[0] = {} + expect(observableArr).toEqual([{}]) + + const observableObj = observable({} as any) + // @ts-ignore + observableObj.__proto__ = Object.create(Object.prototype) + observableObj.aa = {} + expect(observableObj).toEqual({ aa: {} }) +}) diff --git a/packages/reactive/src/handlers.ts b/packages/reactive/src/handlers.ts index 40e879e00f3..62db006479f 100644 --- a/packages/reactive/src/handlers.ts +++ b/packages/reactive/src/handlers.ts @@ -7,13 +7,12 @@ import { isObservable, isSupportObservable } from './externals' import { createObservable } from './internals' const wellKnownSymbols = new Set( - Object.getOwnPropertyNames(Symbol) - .reduce((buf: Symbol[], key) => { - if (key === 'arguments' || key === 'caller') return buf - const value = Symbol[key] - if (typeof value === 'symbol') return buf.concat(value) - return buf - }, []) + Object.getOwnPropertyNames(Symbol).reduce((buf: Symbol[], key) => { + if (key === 'arguments' || key === 'caller') return buf + const value = Symbol[key] + if (typeof value === 'symbol') return buf.concat(value) + return buf + }, []) ) const hasOwnProperty = Object.prototype.hasOwnProperty @@ -201,6 +200,11 @@ export const baseHandlers: ProxyHandler = { return keys }, set(target, key, value, receiver) { + // vue2中有对数组原型重写,因此需去除此处proxy + if (key === '__proto__') { + target[key] = value + return true + } const hadKey = hasOwnProperty.call(target, key) const newValue = createObservable(target, key, value) const oldValue = target[key]