简体   繁体   English

从另一个方法调用对象的方法

[英]Call object's method from another method

I have some module file:我有一些模块文件:

const dbModule = module.exports = {
  setBase: (apiKey, base) => new Airtable({ apiKey }).base(base),
  base: dbModule.setBase('', '')
}

But I get an error message during compile: variable 'dbModule' used before its declaration .但我在编译过程中收到一条错误消息: variable 'dbModule' used before its declaration

So how can I define base method to call setBase ?那么如何定义调用setBase base方法呢?

Rather than declare the variable you are exporting as one step, you could separate it out into multiple steps:与其将要导出的变量声明为一个步骤,不如将其分成多个步骤:

const dbModule = {
  setBase: (apiKey, base) => ....
};

dbModule.base = dbModule.setBase('', '');

module.exports = dbModule;

I'm assuming you have defined dbModule elsewhere in your code.我假设您已经在代码的其他地方定义了 dbModule。

class dbModule {

  setBase() {
  ....
  }
  base() {
    setBase();
  }
};
module.exports = new dbModule();

This class based programming makes sure that each of your functions belong to the class object and can access one another.这种基于类的编程确保您的每个函数都属于类对象并且可以相互访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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