简体   繁体   English

Nodejs:不要使用导出运行“新类”

[英]Nodejs: Don't run `new class` with export

this is my config:这是我的配置:

const one = new one()
const two = new two()

export default {
  one,
  two
}

And this is my classes:这是我的课程:

export class one {
  constructor () {
    console.log("one");
  }
}

export class two {
  constructor () {
    console.log("two");
  }
}

And this is my setup:这是我的设置:

import runner from "./";
runner.one

Why after call runner.one , also runner.two running?!为什么在调用runner.one之后, runner.two也在运行?!

I want to run only runner.one我只想跑runner.one

It happens in here:它发生在这里:

const one = new one()
const two = new two()

cause You defined in constructor() methods of both classes to do console.log因为您在两个类的constructor()方法中定义了console.log

when You call new ClassNameHere() it executes constructor()当您调用new ClassNameHere()时,它会执行constructor()

You could simply export classes instead of instantiating them:您可以简单地导出类而不是实例化它们:

runners.mjs跑者.mjs

export class one {
  constructor () {
    console.log("one");
  }
}

export class two {
  constructor () {
    console.log("two");
  }
}

executor.mjs执行器.mjs

import * as runners from "./runners";

const one = new runner.one();

OR name classes CapitalizedCamelCased:或名称类 CapitalizedCamelCased:

runners.mjs跑者.mjs

export class RunnerOne {
  constructor () {
    console.log("one");
  }
}

export class RunnerTwo {
  constructor () {
    console.log("two");
  }
}

executor.mjs执行器.mjs

import {RunnerOne} from "./runners";

const one = new RunnerOne();

It's happing because you're instantiating both of them globally using const specifier.很高兴,因为您正在使用const说明符全局实例化它们。

When you do this, the code calls the constructor of each class as soon as it comes across instantiation ( new keyword).执行此操作时,代码会在遇到实例化( new关键字)时立即调用每个类的构造函数。

const one = new one()
const two = new two()

export default {
  one,
  two
}

If this is not what you want, and you want them to initialize separately, there could be multiple ways to achieve that.如果这不是您想要的,并且您希望它们分别初始化,则可以有多种方法来实现。

One is to separately instantiate them, before you access them.一种是在访问它们之前单独实例化它们。 (You could use it in a situation where you still need access to instantiated object inside the class you're instantiating them in). (您可以在仍然需要访问实例化对象的类中的实例化对象的情况下使用它)。 Something like this:像这样:

Class that contains one and two:包含一和二的类:

(assuming you define your classes in file onetwo.mjs ) (假设您在文件onetwo.mjs中定义您的类)

import * as ot from "./onetwo.mjs";

let one;
let two;

function instantiateOne() {
  one = new ot.one();
}

function instantiateTwo() {
  two = new ot.two();
}

export default {
  one,
  two,
  instantiateOne,  // expose methods needed to create instances
  instantiateTwo,
}

Class that uses above:上面使用的类:

import runner from "./";

runner.instantiateOne(); // this will only create instance of 'one'

runner.one; // accessing one

Of course there are other ways, and it might need more changes to make it work in your project, but this is one of the patterns.当然还有其他方法,可能需要更多更改才能使其在您的项目中工作,但这是其中一种模式。 Just be aware when using such a strategy, you either always check before using the object ( one or two ) that they have been instantiated, or you ensure you do it at start.请注意,在使用这种策略时,您要么总是在使用对象( onetwo )之前检查它们是否已被实例化,要么确保在开始时就这样做。

NOTE: This is just to give you an idea.注意:这只是给你一个想法。 This isn't a concrete implementation.这不是一个具体的实现。 If you do want to implement something like this, you'll have to handle all edge cases (example, prevent re-initialization etc.)如果你确实想实现这样的东西,你必须处理所有边缘情况(例如,防止重新初始化等)

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

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