]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore: allow custom assignment to this
authorEvan You <yyx990803@gmail.com>
Wed, 21 Aug 2019 13:50:20 +0000 (09:50 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 21 Aug 2019 13:50:20 +0000 (09:50 -0400)
packages/runtime-core/src/component.ts
packages/runtime-core/src/componentProxy.ts

index 25c2f6b70449be013245c2d0720504698e02aa41..7247181151932e94392b0aee527b83e4e13fcadd 100644 (file)
@@ -115,6 +115,9 @@ export type ComponentInstance<P = Data, S = Data> = {
   renderProxy: ComponentRenderProxy | null
   propsProxy: P | null
   setupContext: SetupContext | null
+
+  // user namespace
+  user: { [key: string]: any }
 } & SetupContext &
   LifecycleHooks
 
@@ -198,6 +201,9 @@ export function createComponentInstance(
     slots: EMPTY_OBJ,
     refs: EMPTY_OBJ,
 
+    // user namespace for storing whatever the user assigns to `this`
+    user: {},
+
     emit: (event: string, ...args: unknown[]) => {
       const props = instance.vnode.props || EMPTY_OBJ
       const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
index 348740a34dd4577537661208d622277cc492eb0f..2d37da1fa3b878df37bab47fd802684d9b495bf6 100644 (file)
@@ -26,7 +26,7 @@ export const RenderProxyHandlers = {
         case '$emit':
           return target.emit
         default:
-          break
+          return target.user[key]
       }
     }
   },
@@ -35,15 +35,15 @@ export const RenderProxyHandlers = {
     if (data.hasOwnProperty(key)) {
       data[key] = value
       return true
+    } else if (key[0] === '$' && key.slice(1) in target) {
+      // TODO warn attempt of mutating public property
+      return false
+    } else if (key in target.props) {
+      // TODO warn attempt of mutating prop
+      return false
     } else {
-      if (__DEV__) {
-        if (key[0] === '$') {
-          // TODO warn attempt of mutating public property
-        } else if (target.props.hasOwnProperty(key)) {
-          // TODO warn attempt of mutating prop
-        }
-      }
+      target.user[key] = value
+      return true
     }
-    return false
   }
 }