[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Best way to pass options to a module at require time?
- From: Benoit Germain <bnt.germain@...>
- Date: Thu, 10 Mar 2011 18:13:28 +0100
2011/3/10 Tony Finch <email@example.com>
The reason for this is that require() can be called any number of times
for the same module and when it is called the second and subsequent times
it just returns the existing instance of the module.
The same applies to a
If I changed Lanes for this to become the 'standard' way of initializing Lanes, then it could be encountered more than once with different parameters. The only difference is that when used, this idiom guarantees that the init function gets called everytime the module is required, and not only the first time, which offers a means of complaining if different parameters are passed. In the case of Lanes, it happens that this parameter only serves to improve performance, but has no effect on available functionality, so it isn't that important. On the other hand, the above idiom has the drawback of the init call being mandatory: I can complete initializations and make the module usable only after the init parameter has been provided. In other words, forgetting to call the init function will leave the module in an unusable state, which would not the case if the parameter were forwarded by the call to require.