0

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?

Coyote
  • 2,454
  • 26
  • 47
  • possible duplicate of [How can I share code between Node.js and the browser?](http://stackoverflow.com/questions/3225251/how-can-i-share-code-between-node-js-and-the-browser) – leepowers Dec 31 '12 at 02:05

1 Answers1

2

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • On my side with node.js v0.8.15 it doesn't work: `src/server.js:3 var m = new MyClass(); ^ ReferenceError: MyClass is not defined` which version of node.js are you using? – Coyote Dec 31 '12 at 02:18
  • Sorry, try `MyClass = function () {` instead of `function MyClass()`. – Brad Dec 31 '12 at 02:32
  • @icktoofay, Modules don't get their own global scope. Global is global to the entire application. http://nodejs.org/api/globals.html#globals_global I see what you're getting at though. Let me check my code again and see why this works for me. – Brad Dec 31 '12 at 02:40
  • @icktoofay, I just tested, and it works fine. However, there may be a path issue... code was missing the `./`. editing now. – Brad Dec 31 '12 at 02:44
  • @icktoofay, No worries, but I am a bit confused myself now! It was my understanding that `function something` was equivalent to `something = function`. Any thoughts on that? – Brad Dec 31 '12 at 02:45
  • @Brad: `something = function(...` without a `var` will make the variable be global. `function something` is equivalent to `var something = function(...`. I thought each module had its own global scope, but I guess that was wrong; each module has its own top-level scope, but when variables are assigned when left undeclared, it goes global for all modules. – icktoofay Dec 31 '12 at 02:47
  • Indeed! This way it works. Now to include other files from `MyClass.js` I should use RequireJS? – Coyote Dec 31 '12 at 02:56