Reputation: 2524
I am used to cross platform dev. On the C/C++ side it's simple, but I'm stuck with a problem when using javascript. I want to be able to reuse code from my web-service on the client-side. Currently node.js requires me to write my program in a most unpleasant way but I can handle it:
//MyClass.js
function MyClass()
{
this.test = function(){}
}
module.exports.MyClass = MyClass;
//server.js
var m = new require('MyClass.js').MyClass();
m.test();
//client.js
$.getScript("MyClass.js", function(){
var m = new MyClass();
m.test();
});
To that point it is all fine, but I have an issue when I need to load classes from MyClass.js
. How can I make it work across all the platforms? Is there a way to achieve reusability without processing the files?
Upvotes: 0
Views: 227
Reputation: 163622
Node requires you to do nothing. If you aren't a fan of the way the Node module system works, simply don't use it.
//MyClass.js
MyClass = function ()
{
this.test = function(){}
}
//server.js
require('./MyClass.js');
var m = new MyClass();
m.test();
Now your application is compatible with what you have going on client-side. Just bear in mind that you are now creating classes in the global namespace, which is one reason for using Node's module layout.
I suggest also looking into some of the ways to use a Node-style require
on the client. There are many scripts available, such as RequireJS or Browserify.
Upvotes: 2