From: Evan You Date: Fri, 6 Sep 2019 00:48:14 +0000 (-0400) Subject: refactor: extract hasOwn helper X-Git-Tag: v3.0.0-alpha.0~816 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=891f21b010554137466552486e24389f8590fad2;p=thirdparty%2Fvuejs%2Fcore.git refactor: extract hasOwn helper --- diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index a25e528a87..7b64c6ed6c 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -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) { diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index d30f89728d..dfd621367d 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -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) } } diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 745fcc8e14..488c84704c 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -8,7 +8,8 @@ import { isFunction, isArray, isObject, - isReservedProp + isReservedProp, + hasOwn } from '@vue/shared' import { warn } from './warning' import { Data, ComponentInstance } from './component' @@ -123,7 +124,7 @@ export function resolveProps( if (isReservedProp(key)) continue // any non-declared data are put into a separate `attrs` object // for spreading - if (hasDeclaredProps && !options.hasOwnProperty(key)) { + if (hasDeclaredProps && !hasOwn(options, key)) { ;(attrs || (attrs = {}))[key] = rawProps[key] } else { setProp(key, rawProps[key]) @@ -135,8 +136,8 @@ export function resolveProps( for (const key in options) { let opt = options[key] if (opt == null) continue - const isAbsent = !props.hasOwnProperty(key) - const hasDefault = opt.hasOwnProperty('default') + const isAbsent = !hasOwn(props, key) + const hasDefault = hasOwn(opt, 'default') const currentValue = props[key] // default values if (hasDefault && currentValue === undefined) { @@ -173,7 +174,7 @@ export function resolveProps( ) { const rawInitialProps = toRaw(propsProxy) for (const key in rawInitialProps) { - if (!props.hasOwnProperty(key)) { + if (!hasOwn(props, key)) { delete propsProxy[key] } } diff --git a/packages/runtime-core/src/componentProxy.ts b/packages/runtime-core/src/componentProxy.ts index 019e420453..716df57795 100644 --- a/packages/runtime-core/src/componentProxy.ts +++ b/packages/runtime-core/src/componentProxy.ts @@ -1,16 +1,16 @@ import { ComponentInstance } from './component' import { nextTick } from './scheduler' import { instanceWatch } from './apiWatch' -import { EMPTY_OBJ } from '@vue/shared' +import { EMPTY_OBJ, hasOwn } from '@vue/shared' export const RenderProxyHandlers = { get(target: ComponentInstance, key: string) { const { renderContext, data, props, propsProxy } = target - if (data !== EMPTY_OBJ && data.hasOwnProperty(key)) { + if (data !== EMPTY_OBJ && hasOwn(data, key)) { return data[key] - } else if (renderContext.hasOwnProperty(key)) { + } else if (hasOwn(renderContext, key)) { return renderContext[key] - } else if (props.hasOwnProperty(key)) { + } else if (hasOwn(props, key)) { // return the value from propsProxy for ref unwrapping and readonly return (propsProxy as any)[key] } else { @@ -53,9 +53,9 @@ export const RenderProxyHandlers = { }, set(target: ComponentInstance, key: string, value: any): boolean { const { data, renderContext } = target - if (data !== EMPTY_OBJ && data.hasOwnProperty(key)) { + if (data !== EMPTY_OBJ && hasOwn(data, key)) { data[key] = value - } else if (renderContext.hasOwnProperty(key)) { + } else if (hasOwn(renderContext, key)) { renderContext[key] = value } else if (key[0] === '$' && key.slice(1) in target) { // TODO warn attempt of mutating public property diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 469826aebd..19cca5ef68 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -15,6 +15,12 @@ export const extend = ( return a as any } +const hasOwnProperty = Object.prototype.hasOwnProperty +export const hasOwn = ( + val: object, + key: string | symbol +): key is keyof typeof val => hasOwnProperty.call(val, key) + export const isArray = Array.isArray export const isFunction = (val: any): val is Function => typeof val === 'function'