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 ...

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.