0

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))
1
  • 1
    You're returning the string "b" from the Proxy. "b" is indeed not a function. Commented Aug 26, 2020 at 13:22

1 Answer 1

1

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"}
  },
Sign up to request clarification or add additional context in comments.

1 Comment

I think my answer is a bit primitive but it works. I saw later this discussion with a nice solution. stackoverflow.com/questions/25069023/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.