简体   繁体   English

了解有关JavaScript类的module.exports

[英]Understanding module.exports in regard to JavaScript Classes

While I'm not huge into forcing OOP into a functional language, I'm struggling to understand what a peer of mine did to export modules from a Class. 尽管我并没有强迫OOP成为一种功能语言,但我仍在努力理解我的同辈为从Class导出模块所做的事情。 All I'm looking for is what to call this so I can continue to do research. 我所要寻找的就是这种称呼,因此我可以继续进行研究。 From what I understand they were including some external SDK to be passed into an internal class which would inherit everything into the LibIncludes. 据我了解,它们包括一些要传递给内部类的外部SDK,该内部类会将所有内容继承到LibIncludes中。 This is suppose to allow all of the classes to inherit into the LibIncludes and LibIncludes inherits from Object. 假定这允许所有类继承到LibIncludes中,并且LibIncludes从Object继承。 What I'm running into now is that anytime I try to call LibIncludes.Handlers; 我现在遇到的是任何时候我尝试调用LibIncludes.Handlers;。 and execute a function included in Handlers I'm getting undefined. 并执行我未定义的处理程序中包含的功能。 I can't figure out what pattern he applied to make this work and everything in ES6 shows an entirely different approach to exporting and importing between classes for inheritance. 我不知道他采用了什么模式来完成这项工作,ES6中的所有内容都显示了在类之间进行继承的完全不同的导出和导入方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。

// index.js
const ExternalSdk = new SDK.Function();
const HandlerService = require('./hander/handler.js');

class LibIncludes {
  static compose() {
    const Handler = new HandlerSvc(ExternalSdk);
    return {
    Handler
    };
  }
}

module.exports = LibIncludes;

let function = LibIncludes.sdkFunciton;
// function should be invoked and return some object

Here's the solution I came up with for refactoring. 这是我提出的用于重构的解决方案。 It seems cleaner and without the static members: 似乎更干净,并且没有静态成员:

// index.js // index.js

const ExternalSdk = new SDK.Function();
const HandlerService = require('./hander/handler.js');

module.exports = class LibIncludes {
  constructor(handler) {
    this.handler = new HandlerSvc(ExternalSdk);
  }
}

// app.js // app.js

const lib = require('./index');

var getSomething = new lib();
console.log(getSomething.handler);
// function should be invoked and return some object

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

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