2

Sometimes I've seen people use module.exports.instance in their code (example). How does this differ from module.exports?

powerboy
  • 10,523
  • 20
  • 63
  • 93
  • It's a property being added to the `exports` object, just like any other property. They're assigning an instance of the `ConnectAssets` constructor to it. Nothing special really. It has no special meaning WRT NodeJS API. –  Jul 30 '12 at 03:28

1 Answers1

2

The literal difference is that module.exports.instance is a property of the object referenced by module.exports.

Why use a property named instance? One possibility is an application of the Singleton Pattern to resolve issues with circular dependencies among modules.

Community
  • 1
  • 1
CgodLEY
  • 994
  • 8
  • 16
  • So that I can require that module and attache new property to that module? For instance, `var connectAssets = require('connect-assets'); connectAssets.instance.foo = 'bar';` – powerboy Jul 30 '12 at 05:51
  • You can assign any valid property you'd like to a module. For example `var module = require('module'); module.foo = 'bar';` Using `instance` as a property name has no special meaning and it is treated like any other property of the object. I was just giving an example of where you might use a property named `instance`. – CgodLEY Jul 30 '12 at 06:10