簡體   English   中英

無法通過代理對象訪問類方法

[英]Cannot access class method through a proxy object

就像在標題中我無法通過代理對象訪問類方法一樣,我收到錯誤

TypeError: sth.getNumber is not a function

但在我看到它像屬性一樣被訪問之前,因為我在終端中看到“get”登錄

我真的不知道為什么會這樣。 下面是我想要做什么的簡化示例。 預先感謝您的幫助

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

改變:

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

到:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM