diff --git a/src/reactivity/reactive.ts b/src/reactivity/reactive.ts index 827223f6..4691011e 100644 --- a/src/reactivity/reactive.ts +++ b/src/reactivity/reactive.ts @@ -16,13 +16,19 @@ import { rawSet, accessModifiedSet } from '../utils/sets' export function isRaw(obj: any): boolean { return Boolean( - obj?.__ob__ && typeof obj.__ob__ === 'object' && obj.__ob__?.__raw__ + obj && + hasOwn(obj, '__ob__') && + typeof obj.__ob__ === 'object' && + obj.__ob__?.__raw__ ) } export function isReactive(obj: any): boolean { return Boolean( - obj?.__ob__ && typeof obj.__ob__ === 'object' && !obj.__ob__?.__raw__ + obj && + hasOwn(obj, '__ob__') && + typeof obj.__ob__ === 'object' && + !obj.__ob__?.__raw__ ) } diff --git a/test/v3/reactivity/reactive.spec.ts b/test/v3/reactivity/reactive.spec.ts index 4ae2e7de..7719a1e9 100644 --- a/test/v3/reactivity/reactive.spec.ts +++ b/test/v3/reactivity/reactive.spec.ts @@ -8,6 +8,7 @@ import { shallowReactive, set, markRaw, + isRaw, } from '../../../src' describe('reactivity/reactive', () => { @@ -35,6 +36,22 @@ describe('reactivity/reactive', () => { expect(Object.keys(observed)).toEqual(['foo']) }) + //#693 + test('the hasOwn should be used to determine whether an attribute exists.', () => { + const obj = {} + expect(isReactive(obj)).toBe(false) + expect(isRaw(obj)).toBe(false) + const mockObj = new Proxy(obj, { + get: (target, key) => { + if (!(key in Object.keys(target))) { + throw new Error(`the ${key.toString()} is not found in the target.`) + } + }, + }) + expect(isReactive(mockObj)).toBe(false) + expect(isRaw(obj)).toBe(false) + }) + test('proto', () => { const obj = {} const reactiveObj = reactive(obj)