Skip to content

Commit 65abcb4

Browse files
authored
fix: the hasOwn should be used to determine whether an attribute exists. (#737)
1 parent 14d1c7b commit 65abcb4

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/reactivity/reactive.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ import { rawSet, accessModifiedSet } from '../utils/sets'
1616

1717
export function isRaw(obj: any): boolean {
1818
return Boolean(
19-
obj?.__ob__ && typeof obj.__ob__ === 'object' && obj.__ob__?.__raw__
19+
obj &&
20+
hasOwn(obj, '__ob__') &&
21+
typeof obj.__ob__ === 'object' &&
22+
obj.__ob__?.__raw__
2023
)
2124
}
2225

2326
export function isReactive(obj: any): boolean {
2427
return Boolean(
25-
obj?.__ob__ && typeof obj.__ob__ === 'object' && !obj.__ob__?.__raw__
28+
obj &&
29+
hasOwn(obj, '__ob__') &&
30+
typeof obj.__ob__ === 'object' &&
31+
!obj.__ob__?.__raw__
2632
)
2733
}
2834

test/v3/reactivity/reactive.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
shallowReactive,
99
set,
1010
markRaw,
11+
isRaw,
1112
} from '../../../src'
1213

1314
describe('reactivity/reactive', () => {
@@ -35,6 +36,22 @@ describe('reactivity/reactive', () => {
3536
expect(Object.keys(observed)).toEqual(['foo'])
3637
})
3738

39+
//#693
40+
test('the hasOwn should be used to determine whether an attribute exists.', () => {
41+
const obj = {}
42+
expect(isReactive(obj)).toBe(false)
43+
expect(isRaw(obj)).toBe(false)
44+
const mockObj = new Proxy(obj, {
45+
get: (target, key) => {
46+
if (!(key in Object.keys(target))) {
47+
throw new Error(`the ${key.toString()} is not found in the target.`)
48+
}
49+
},
50+
})
51+
expect(isReactive(mockObj)).toBe(false)
52+
expect(isRaw(obj)).toBe(false)
53+
})
54+
3855
test('proto', () => {
3956
const obj = {}
4057
const reactiveObj = reactive(obj)

0 commit comments

Comments
 (0)