Is it guaranteed that export default new object()
, where object is some kind of type (e.g. Date), returns the same object all the time?
// date.js
export default new Date()
// foo.js
import date from './date'
// bar.js
import date from './date'
Can it be expected that date
in foo.js
and date
in bar.js
are equivalent. So import date from './date'
is a singleton?
Would the above export default new Date()
be equivalent to module.exports = new Date()
for pre-es6 era?
Yes, it will always be the same object. In any given run of a script, you can assume that the top level of a file, eg:
// date.js
export default new Date()
will run exactly once, and no more, if the file gets imported. Further imports of that file will result in date.js
's exports being importable, but will not run date.js
again.