* Creates a new instance by given identifier. * @param {string} identifier * @param {...mixed} args Arguments passed to the invokable constructor * @return {object} Instantiated object
(identifier, ...args)
| 68 | * @return {object} Instantiated object |
| 69 | */ |
| 70 | create (identifier, ...args) { |
| 71 | if (!this.exists(identifier)) { |
| 72 | throw new Error( |
| 73 | `Can't create '${identifier}', invokable has not been registered.`) |
| 74 | } |
| 75 | |
| 76 | const index = this._identifiers.indexOf(identifier) |
| 77 | const invokable = this._invokables[index] |
| 78 | |
| 79 | // the following lines do basically this: new invokable(...args) |
| 80 | args.splice(0, 0, invokable) |
| 81 | return new (Function.prototype.bind.apply(invokable, args))() |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns factory singleton instance. |
no test coverage detected