]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): revert to Reflect.get and add test cases
authorEvan You <yyx990803@gmail.com>
Fri, 18 Oct 2019 19:31:28 +0000 (15:31 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 18 Oct 2019 19:31:28 +0000 (15:31 -0400)
packages/reactivity/__tests__/effect.spec.ts
packages/reactivity/src/baseHandlers.ts

index 340bf10931e083f1642edc0667fac19ae1591a31..32a7fbdfea29f94a59b2c69f6e528f34166405c6 100644 (file)
@@ -249,6 +249,36 @@ describe('reactivity/effect', () => {
     expect(dummy).toBe(newFunc)
   })
 
+  it('should observe chained getters relying on this', () => {
+    const obj = reactive({
+      a: 1,
+      get b() {
+        return this.a
+      }
+    })
+
+    let dummy
+    effect(() => (dummy = obj.b))
+    expect(dummy).toBe(1)
+    obj.a++
+    expect(dummy).toBe(2)
+  })
+
+  it('should observe methods relying on this', () => {
+    const obj = reactive({
+      a: 1,
+      b() {
+        return this.a
+      }
+    })
+
+    let dummy
+    effect(() => (dummy = obj.b()))
+    expect(dummy).toBe(1)
+    obj.a++
+    expect(dummy).toBe(2)
+  })
+
   it('should not observe set operations without a value change', () => {
     let hasDummy, getDummy
     const obj = reactive({ prop: 'value' })
index 4ebdb0a3354d3bf01b248a7a63298dde90ff6d1b..0043af73fe5cc759c8d8593cfc5fb05f6b9bfb6e 100644 (file)
@@ -12,9 +12,8 @@ const builtInSymbols = new Set(
 )
 
 function createGetter(isReadonly: boolean) {
-  return function get(target: any, key: string | symbol) {
-    // not using Reflect.get here for perf reasons
-    const res = target[key]
+  return function get(target: any, key: string | symbol, receiver: any) {
+    const res = Reflect.get(target, key, receiver)
     if (isSymbol(key) && builtInSymbols.has(key)) {
       return res
     }