]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: extract hasOwn helper
authorEvan You <yyx990803@gmail.com>
Fri, 6 Sep 2019 00:48:14 +0000 (20:48 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 6 Sep 2019 00:48:14 +0000 (20:48 -0400)
packages/reactivity/src/baseHandlers.ts
packages/reactivity/src/collectionHandlers.ts
packages/runtime-core/src/componentProps.ts
packages/runtime-core/src/componentProxy.ts
packages/shared/src/index.ts

index a25e528a87c9c3d4796354ab4c9605fd5759e306..7b64c6ed6ca60fb44c77a47c9952e5b7758c94b7 100644 (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) {
index d30f89728db8d006a19ce1c8ca9a52cbb0d223d2..dfd621367d61f89433f2d826c2d3511109529ae0 100644 (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)
   }
 }
index 745fcc8e141b9b9c4ec1201eb26791baf5d9637f..488c84704cbacc4d54746beaa1471dfe483947c3 100644 (file)
@@ -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]
       }
     }
index 019e420453af655cc7146b3896b4a609e9c06d30..716df5779554f17ace29f6c6d616dd64a683efe8 100644 (file)
@@ -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
index 469826aebd1243e7936f2a49a0d6dcbf3d2135f2..19cca5ef68c35bada403fd5708cf7e621a0c2548 100644 (file)
@@ -15,6 +15,12 @@ export const extend = <T extends object, U extends object>(
   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'