Using the object-oriented modules
What if we want to use our module as an object with a specific type similar to a class? We can also do this with PhantomJS. Instead of using exports
, we use module.exports
. So what is the difference between the two? Exports are like a typical module instance that can be directly accessed like an instantiated object, but the latter is a blueprint or class that we can instantiate, and has a specific object type and properties.
Tip
Learn more about CommonJS and its concepts by checking out the CommonJS wiki page at http://wiki.commonjs.org/.
So how do we create module.exports
? Let's try one example and build it up as we progress.
First, we create our module. Let's model a person as our object and name our module as person.js
.
module.exports = function() { };
The code above represents a module, although it doesn't give any value. Instead of using exports
, we define our function with module.exports
. Actually, the function definition is our object constructor...