Skip to content
wade.one

wade.one

wade womersley – york based software engineer

  • Home
  • 2013
  • December
  • 9
  • phantomjs custom module – require and create

phantomjs custom module – require and create

Posted on December 9, 2013 By Wade No Comments on phantomjs custom module – require and create
Programming

Recently I’ve been working with phantomjs in order to do some on-page control without wanting to use an actual browser (phantomjs is headless and requires no X server to be running).

One of the first things I wanted to do was created custom modules so I could organise my code clearly. I found a surprising lack of information on this topic so decided to quickly write up how I did it so it follows how the included modules work.

To use a custom module, you need to prefix the require with a path (./ works fine for the current directory) and exclude the .js extension:

var mymodule = require('./mymodule');

So in this case, alongside your primary JS file, you’d have a mymodule.js file.

Next, in mymodule.js, you’d write code something like this:

function MyCustomModule(){
...your code here...
}

MyCustomModule.prototype.someFunction = function(){
...your function code...
};

exports.create = function(){
    return new MyCustomModule();
};

This will allow you to return a new instance of your MyCustomModule class while using prototyping.

So going backing to the original require, to create a new instance you could do the following:

var customModule = require('./mymodule').create();

It’s really simple and obvious once you see it, there just was no where to see it!

Share:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Post navigation

❮ Previous Post: C# MySqlDataReader – “one line” function to return a row as an instance of a class.
Next Post: Ultra High Definition TV Coming Soon ❯

You may also like

Programming
Using SSL in RestClient
March 4, 2011
Programming
Reset MySQL root password if you forgot it #mysql
April 12, 2010
Programming
Hidden things, lack of fries and pointless data
September 4, 2009
PHP
Expanding on performance of Redis vs MongoDB for a push/pull system
March 26, 2023

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Copyright © 2025 wade.one.

Theme: Oceanly News Dark by ScriptsTown