Javascript 要求将方法导出到类中的正确方法(原型?)

Javascript 要求将方法导出到类中的正确方法(原型?),javascript,node.js,class,ecmascript-6,module,Javascript,Node.js,Class,Ecmascript 6,Module,我在重构我的东西时遇到了一个小问题 问题: 我得到了一个包含1.1k行和~10个方法的类文件 我想做什么(tl;dr): 将每个方法外包到它自己的文件module.export中,创建一个需要所有方法的助手方法/文件,让我的类只导入这个助手文件,并将所有方法添加回我的类中,以便class.method正确调用它们 我想做什么(外加一点) Aight-状态是。。。我有课 myclass.js class MyClass{ constructor(a,b){ this.a = a

我在重构我的东西时遇到了一个小问题

问题:

我得到了一个包含1.1k行和~10个方法的类文件

我想做什么(tl;dr):

将每个方法外包到它自己的文件module.export中,创建一个需要所有方法的助手方法/文件,让我的类只导入这个助手文件,并将所有方法添加回我的类中,以便class.method正确调用它们

我想做什么(外加一点)

Aight-状态是。。。我有课

myclass.js

class MyClass{
  constructor(a,b){
    this.a = a
    this.b = b
  }

  methodOne(){
  console.log(this.a)
  *100 lines of code*
  }

  methodTwo(){
  *100 lines of code*
  }

  methodThree(){
  *100 lines of code*
  }
   .... and so on
}
 module.exports.methodOne = () => {
   console.log(this.a) 
   99 more lines of Code
 }
 module.exports.methodTwo = () => {
    100 lines of Code
 }
const functions = require('./functions_injector')
class MyClass {
  constructor(a,b) {
    this.a = a
    this.b = b
 }
}

const funcNames = Object.keys(functions)
const funcs = Object.values(functions)
funcNames.forEach((funcName, idx) => {
  MyClass.prototype[funcName] = funcs[idx]
}

const tryMe = new MyClass('a', 'b)
const plsWork = tryMe.methodOne()
console.log(plsWork)
我想要的:

methodOne.js

class MyClass{
  constructor(a,b){
    this.a = a
    this.b = b
  }

  methodOne(){
  console.log(this.a)
  *100 lines of code*
  }

  methodTwo(){
  *100 lines of code*
  }

  methodThree(){
  *100 lines of code*
  }
   .... and so on
}
 module.exports.methodOne = () => {
   console.log(this.a) 
   99 more lines of Code
 }
 module.exports.methodTwo = () => {
    100 lines of Code
 }
const functions = require('./functions_injector')
class MyClass {
  constructor(a,b) {
    this.a = a
    this.b = b
 }
}

const funcNames = Object.keys(functions)
const funcs = Object.values(functions)
funcNames.forEach((funcName, idx) => {
  MyClass.prototype[funcName] = funcs[idx]
}

const tryMe = new MyClass('a', 'b)
const plsWork = tryMe.methodOne()
console.log(plsWork)
methodTwo.js

class MyClass{
  constructor(a,b){
    this.a = a
    this.b = b
  }

  methodOne(){
  console.log(this.a)
  *100 lines of code*
  }

  methodTwo(){
  *100 lines of code*
  }

  methodThree(){
  *100 lines of code*
  }
   .... and so on
}
 module.exports.methodOne = () => {
   console.log(this.a) 
   99 more lines of Code
 }
 module.exports.methodTwo = () => {
    100 lines of Code
 }
const functions = require('./functions_injector')
class MyClass {
  constructor(a,b) {
    this.a = a
    this.b = b
 }
}

const funcNames = Object.keys(functions)
const funcs = Object.values(functions)
funcNames.forEach((funcName, idx) => {
  MyClass.prototype[funcName] = funcs[idx]
}

const tryMe = new MyClass('a', 'b)
const plsWork = tryMe.methodOne()
console.log(plsWork)
……等等

函数\u injector.js-长度有点假

const fs = require('fs')
const functions = {}
const functionsInjector = () => {
 fs.readDir('./functions/', (err, files) => {
   files.each((fileName) => {
   functions[fileName.split('.')[0]] = require('./functions/'+fileName)       
 }
}
functionsInjector()
module.exports = functions
myclass.js

class MyClass{
  constructor(a,b){
    this.a = a
    this.b = b
  }

  methodOne(){
  console.log(this.a)
  *100 lines of code*
  }

  methodTwo(){
  *100 lines of code*
  }

  methodThree(){
  *100 lines of code*
  }
   .... and so on
}
 module.exports.methodOne = () => {
   console.log(this.a) 
   99 more lines of Code
 }
 module.exports.methodTwo = () => {
    100 lines of Code
 }
const functions = require('./functions_injector')
class MyClass {
  constructor(a,b) {
    this.a = a
    this.b = b
 }
}

const funcNames = Object.keys(functions)
const funcs = Object.values(functions)
funcNames.forEach((funcName, idx) => {
  MyClass.prototype[funcName] = funcs[idx]
}

const tryMe = new MyClass('a', 'b)
const plsWork = tryMe.methodOne()
console.log(plsWork)
预期为“a”

可悲的是,现实是:没有定义

通过调用methodOne.js中的console.log(this)进行深入挖掘,返回{methodOne:[Function]),这有什么意义呢

const tryMe = new MyClass()
tryMe.methodOne()
我至少可以确认,我可以通过类访问这些方法,现在它们只需要访问相同的类

代码有点简化。真正的代码是完全异步/等待之类的,但是我不想用更多的代码来打扰你

注入器的构造并将其传递给myClass正在工作,我可以访问{name:function}对的对象。我所需要的只是一点提示,告诉我如何将这些方法添加到我的类中,使它们成为类的方法

非常感谢任何帮助!干杯

PS:我可能可以让每个方法都成为自己的类,然后扩展,再扩展,再扩展…但是因为我想让每个方法都在自己的文件中(我知道这是一个风格问题,但是呃),我真的不想走这条路

我仍在考虑我是否在方法的module.exports端做了一些错误的事情。如果我做了,我绝对没有得到任何线索:D

此方法有效,但它无法访问
This.a
This.b

而且,您的
MyClass
中有一个输入错误,它是
构造函数

简单的概念证明: 简单代码示例:

const methodOne = function () {
  console.log(this.a);
}

class MyClass {
  constructor(a, b) {
    this.a = a;
    this.b = b;
  }
  m2() {
    console.log(this.b);
  }
}
MyClass.prototype.methodOne = methodOne;

const tryMe = new MyClass('I work', 'I work too');
tryMe.methodOne()
tryMe.m2();
输出:

I work
I work too

不要使用箭头函数,请使用普通函数。箭头函数有一个词汇绑定的
上下文,因此它永远不会引用您的对象-普通函数将在执行时设置
。因此,只需将
module.exports.methodOne=()=>{
更改为
module.exports.methodOne=函数(){
我以前也尝试过。假设我在myClass中添加了一个方法,只需记录Object.keys(这个),重写methodOne以使用普通函数声明并记录Object.keys(这个)也在那里…myClass日志['a','b],methodOne仍然未定义:(编辑:但它让我更接近..而带有箭头函数的console.log(这个)返回[methodOne:[Function]],console.log(this)和函数声明返回[Function:MyClass]。但我仍然无法访问实例thisnop,忽略该注释…我只是通过尝试.bind()获得了一些工件在玩普通函数声明时…很有效,干杯!除了arrow函数语法的问题外,导入整个方法定义是错误的。您的根本问题实际上是100个LOC方法。将它们重构为较小的辅助函数,从中定义的方法调用辅助函数
class
body(并将所有值作为单独的参数传递给它们,不要传递整个实例)。然后将助手函数放入各自的模块中。Cheeersss…我过去曾尝试过,但似乎我也在尝试绑定()一些有趣的东西..gnahh。谢谢!