从此沉迷学习 2021-09-12 13:58 采纳率: 100%
浏览 32
已结题

class一个方法中控制返回的其他方法,为什么调用出错

class FieldStore {
      constructor() {
        this.store = {
          a: "sss",
        };
      }
      getFieldValue(name) {
        console.log(this);
        return this.store[name];
      }
      getForm = () => {
        return {
          getFieldValue: this.getFieldValue,
        };
      };
}
const fieldStore = new FieldStore();
console.log(fieldStore.getForm().getFieldValue("a"));

class一个方法中控制返回的其他方法,为什么调用出错?怎么解决

  • 写回答

1条回答 默认 最新

  • 关注
    class FieldStore{
          constructor() {
            this.store = {
              a: "sss",
            };
          }
          getFieldValue(name) {
            console.log(this);
            return this.store[name];
          }
          getForm(){
            return {
              getFieldValue: this.getFieldValue.bind(this),
            };
          }
    }
    const fieldStore = new FieldStore();
    console.log(fieldStore.getForm().getFieldValue("a"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月20日
  • 已采纳回答 9月12日
  • 创建了问题 9月12日