我如何将构造函数参数传递给Ecmascript 6中的导入类



如果我将类导入其他脚本,我该如何将参数传递给ES6语法中的该类构造函数?

我想做这样的事情。我已经看到了各种建议,例如包装功能或使用工厂模式,但是是否有更简单的方法来执行此操作?

// This is sudo code
import SomeClass from './SomeClassPath';
var thing = SomeClass(params);

我看到您的问题有些混乱,所以让我澄清。

在ES6中,您可能知道,当您需要导出模块时,您有两种策略。您可以使用默认导出多个导出。让我们以一个非常基本的示例(围绕console的简单记录器(:

function info(msg) {
  console.info(`[Info] ${msg}`);
}
function error(msg) {
  console.error(`[Error] ${msg)`);
}

默认导出

在这里,我们必须分组我们的功能。在JavaScript中执行此操作的最愚蠢的方法是使用对象字面的(请参阅揭示模块模式(:

export default {
  info(msg) {
    console.info(`[Info] ${msg}`);
  },
  error(msg) {
    console.error(`[Error] ${msg}`);
  }
};

然后,在我们的客户端代码中,我们将使用此模块这样:

import logger from './logger'
logger.info('Hello!');
logger.error('Oops!');

多个导出

在这里我们可以独立导出我们的功能:

export function info(msg) {
  console.info(`[Info] ${msg}`);
}
export function error(msg) {
  console.error(`[Error] ${msg}`);
}

然后,在我们的客户端代码中,我们将使用此模块这样:

import {info, error} from './logger'
info('Hello!');
error('Oops!');

完成。

我建议您了解ES6模块系统如何与我们的功能示例一起使用。上课完全是一样的...


singleton?

通过阅读评论,我看到了另一个需要澄清的混乱: singleton

Singleton是一种设计模式,它使得可以将类实例化曾经。现在想象我们的班级如下:

export default class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
};

我们可以这样使用:

import Person from './Person';
let me = new Person('Baptiste', 'Vannesson'),
    you = new Person('David', 'Choi');
console.log(Object.is(me, you)); // false, so there are two instances of Person
console.log(me.firstName, me.lastName);  // Baptiste Vannesson
console.log(you.firstName, you.lastName); // David Choi

您可以看到,人与辛格尔顿无关!这将是一个具有以下Java启发的实现的单身人士:

export default (() => {
  class Person {
    // Private constructor
    constructor(firstName, lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
    }
  }
  return {
    // Public static factory method
    getInstance(firstName, lastName) {
      if (!Person.instance) {
        Person.instance = new Person(firstName, lastName);
      }
      return Person.instance;
    }
  };
})();

客户端代码:

import Person from './Person';
let me = Person.getInstance('Baptiste', 'Vannesson'),
    you = Person.getInstance('David', 'Choi');
console.log(Object.is(me, you)); // true, so there is only one instance
console.log(me.firstName, me.lastName); // Baptiste Vannesson
console.log(you.firstName, you.lastName); // Baptiste Vannesson (still me!)

为了简单起见,您可能希望直接导出一个实例:

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
};
export default new Person('David', 'Choi');

客户端代码:

import person from './person';
// Person is not a constructor but a simple instance
let me = person,
    you = person;
console.log(Object.is(me, you)); // true
console.log(me.firstName, me.lastName); // David Choi
console.log(you.firstName, you.lastName); // David Choi

如果您这样做,那么使用对象字面的对象会更简单:

export default {
  firstName: 'David',
  lastName: 'Choi'
};

客户端代码在此处不更改。

将参数传递到您的ES6模块非常简单且直截了当。只是做这个简单的事情。

// This is sudo code
require('./SomeClassPath.js')(param);

然后在模块文件中someclasspath.js执行此操作

module.exports = function(param) {
 ....     
}

相关内容