Put those class definitions into a file and include into the client with <script>
and require
with nodejs.
To achieve this on nodejs you need to pass the class def into the module.exports
variable, that is only avaiable at nodejs.
var Foo = function (fid) {
this.fid = fid;
this.toJSON = function () {
return ('{"fid":"' + this.fid + '"}');
};
};
if(module && module.exports)
module.exports = Foo;
Then you can use it in nodejs:
Foo = require("foo.js");
var foo = new Foo();
This solution works, if you place every class in an own file.
If you want to have all classes in one file:
var Foo = function (fid) {
this.fid = fid;
this.toJSON = function () {
return ('{"fid":"' + this.fid + '"}');
};
};
var Bar = function (fid) {
this.fid = fid;
this.toJSON = function () {
return ('{"fid":"' + this.fid + '"}');
};
};
if(module && module.exports){
module.exports.Foo = Foo;
module.exports.Bar = Bar;
}
In you nodejs:
var Classes = require("classes.js");
var foo = new Classes.Foo();
Update due to the question in the comment:
To use the Foo
inside the Bar
you need to require
this in nodejs and in client you dont need anything (global).
var Bar = Bar || require("Bar.js"); //if class Bar is undefined, we work on nodejs and need to require the Bar file.
function Foo (){
var bar = new Bar();
}
if(module && module.exports){
module.exports.Foo = Foo;
}