Created
February 22, 2012 13:52
-
-
Save janjongboom/1885247 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MyObject () { | |
// do stuff | |
} | |
// now to export this as a nodejs module do | |
module.exports = MyObject; | |
// ===== | |
// in another file (in the same folder) you can now do | |
var MyObject = require("./MyObject"); | |
// use it just like before | |
var obj = new MyObject(); |
In Summary, here's the changes I needed to make:
- At the top of the class TimeEntry.js, added a require line for the dependency:
var FreshBooks_Element = require('./Element');
-
After the declaration of the function constructor in TimeEntry.js:
function FreshBooks_TimeEntry()
{
this.elementName = "time_entry";
...
}
Added, per http://nodejs.org/api/modules.html#module.exports and http://stackoverflow.com/questions/8296379/create-a-class-in-nodejs:
module.exports = FreshBooks_TimeEntry; -
After the declaration of the function constructor in Element.js:
function FreshBooks_Element()
{
...
}
Added:
module.exports = FreshBooks_Element;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MartinCleaver: Does Node's Require mandate that filenames match classnames? (I am trying to minimally adapt a base javascript library where class FreshBooks_Element comes from file FreshBooks/Element.js)
[10:18am] bradleymeck: MartinCleaver, no
[10:19am] konobi: MartinCleaver: anyways... no... require... look at the "module" docs
[10:20am] konobi: http://nodejs.org/docs/latest/api/modules.html
[10:20am] MartinCleaver: I have http://nodejs.org/api/modules.html#loading_from_node_modules_Folders open
[10:21am] • MartinCleaver is searching for the module naming rules… I think I've seen it in the past
[10:21am] bradleymeck: there is none MartinCleaver, the only thing node cares about is file extensions
[10:21am] konobi: modules are just objects... they can be named anything you want
[10:21am] bradleymeck: you can export anything you want