How to write browser- and node-compatible object oriented code?

217 views
Skip to first unread message

Ptr

unread,
Oct 31, 2010, 4:47:23 PM10/31/10
to nodejs
Hi,

I don't like the fact that I have to write exports.myMethod statements
only to make some methods available in node. The moment I am writing a
class ala:

MyClass = function () {...}
MyClass.prototype.testMethod = function() { ... }
...

and use this class in node and in a browser identically via:
var obj = new MyClass();

in node I only need to call require('./MyClass'); before

I know that var newPackage = require('./MyClass'); would be a bit
better (in terms of conflicting class names).

So, is there a way to make this happen? Or how do you use classes in
node and browser?

Regards,
Peter.

Isaac Schlueter

unread,
Oct 31, 2010, 7:40:31 PM10/31/10
to nod...@googlegroups.com
How about this pattern?


//my-class.js
if (typeof module !== "undefined") module.exports = MyClass
function MyClass () {}
MyClass.prototype.doSomething = function () { console.log("hi") }


// other-file.js
var MyClass = require("./my-class")
var mc = new MyClass()
mc.doSomething()


--i

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

AJ ONeal

unread,
Oct 31, 2010, 11:35:54 PM10/31/10
to nod...@googlegroups.com

Take a look at the post I just made about Example API working in node and browser. I have a stupid simple require module for the browser that I use. It also keeps the global namespace clean.

AJ

Sent from my Google Android

Ptr

unread,
Nov 1, 2010, 6:16:49 AM11/1/10
to nodejs
ah, great! You perfectly understood what i was looking for! Thanks!

I am doing now the slightly changed schlueter-procedure:

MyClass = function () {}
if (typeof module !== "undefined") module.exports = MyClass

otherwise if ClassA uses ClassB it couldn't find ClassB (?)

Kind Regards,
Peter.

On 1 Nov., 00:40, Isaac Schlueter <i...@izs.me> wrote:
> How about this pattern?
>
> //my-class.js
> if (typeof module !== "undefined") module.exports = MyClass
> function MyClass () {}
> MyClass.prototype.doSomething = function () { console.log("hi") }
>
> // other-file.js
> var MyClass = require("./my-class")
> var mc = new MyClass()
> mc.doSomething()
>
> --i
>

Camilo Aguilar

unread,
Nov 1, 2010, 12:49:12 PM11/1/10
to nod...@googlegroups.com
You can also take a look at RequireJS library http://requirejs.org/

Alexis

unread,
Nov 1, 2010, 4:54:18 PM11/1/10
to nodejs
I have two files
file1:
var my_object = function(){
return { first_func: function() {} };
}();

file2:
my_object.new_func = function(){
};

is there anyway to do that in node? I want to be able to add a
function to module in another file.

On Nov 1, 11:49 am, Camilo Aguilar <cam...@cloudescape.com> wrote:
> You can also take a look at RequireJS libraryhttp://requirejs.org/
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/nodejs?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .

Micheil Smith

unread,
Nov 1, 2010, 5:39:44 PM11/1/10
to nod...@googlegroups.com
Yes, although, it's slightly different:

file1:
var my_object = function(){
return { first_func: function() {} };
}();

if(typeof exports !== undefined) exports.my_object = my_object;

file2:
if(typeof exports !== undefined) var my_object = require("file1").my_object;

my_object.new_func = function(){
};

However, that won't necessarily change the original my_object, like it
would in the browser.

Hopefully this helps,

Yours,
Micheil Smith
--
BrandedCode.com

> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages