refactor: extract hasOwn helper

This commit is contained in:
Evan You
2019-09-05 20:48:14 -04:00
parent 7eed0df3c2
commit 891f21b010
5 changed files with 23 additions and 20 deletions

View File

@@ -2,11 +2,9 @@ import { reactive, readonly, toRaw } from './reactive'
import { OperationTypes } from './operations'
import { track, trigger } from './effect'
import { LOCKED } from './lock'
import { isObject } from '@vue/shared'
import { isObject, hasOwn } from '@vue/shared'
import { isRef } from './ref'
const hasOwnProperty = Object.prototype.hasOwnProperty
const builtInSymbols = new Set(
Object.getOwnPropertyNames(Symbol)
.map(key => (Symbol as any)[key])
@@ -40,7 +38,7 @@ function set(
receiver: any
): boolean {
value = toRaw(value)
const hadKey = hasOwnProperty.call(target, key)
const hadKey = hasOwn(target, key)
const oldValue = target[key]
if (isRef(oldValue) && !isRef(value)) {
oldValue.value = value
@@ -69,7 +67,7 @@ function set(
}
function deleteProperty(target: any, key: string | symbol): boolean {
const hadKey = hasOwnProperty.call(target, key)
const hadKey = hasOwn(target, key)
const oldValue = target[key]
const result = Reflect.deleteProperty(target, key)
if (hadKey) {

View File

@@ -2,7 +2,7 @@ import { toRaw, reactive, readonly } from './reactive'
import { track, trigger } from './effect'
import { OperationTypes } from './operations'
import { LOCKED } from './lock'
import { isObject, capitalize } from '@vue/shared'
import { isObject, capitalize, hasOwn } from '@vue/shared'
const toReactive = (value: any) => (isObject(value) ? reactive(value) : value)
const toReadonly = (value: any) => (isObject(value) ? readonly(value) : value)
@@ -222,9 +222,7 @@ function createInstrumentationGetter(instrumentations: any) {
receiver: any
) {
target =
instrumentations.hasOwnProperty(key) && key in target
? instrumentations
: target
hasOwn(instrumentations, key) && key in target ? instrumentations : target
return Reflect.get(target, key, receiver)
}
}