简体   繁体   中英

Cannot access class method through a proxy object

like in the title I can't access class method through a proxy object, I get the error

TypeError: sth.getNumber is not a function

But before I see that It was accessed like property because I see "get" log in the terminal

I don't really know why this is happening. Below it's my simplified example of what I want to do. Thanks in advance for help

class mockClass {
  sth?: number
  constructor(n?: number) {
    this.sth = n
  }
  public getNumber(n: number) {
    return n
  }
}

const sth = new Proxy<any>(new mockClass(15), {
  apply: function (target, thisArg, argArr) {
    console.log("apply")
    console.log(target, thisArg, argArr)
    return "a"
  },
  get: function (target, reciver) {
    console.log("get")
    console.log(target, reciver)
    return "b"
  },
})

console.log(sth.getNumber(15))

Change:

get: function (target, reciver) {
    console.log("get")
    console.log(target, reciver)
    return "b"
  },

To:

get: function (target, reciver) {
    console.log("get")
    console.log(target, reciver)
    return () => { return "b"}
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM