Damian Grzanka
Damian Grzanka

Reputation: 305

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))

Upvotes: 0

Views: 968

Answers (1)

Karlan
Karlan

Reputation: 353

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"}
  },

Upvotes: 1

Related Questions