views:

91

answers:

2

Hi,

I'm working on a JavaScript software that bears resemblance to Windows. It has a desktop, taskbar, etc. and I'm wondering whether I should make the desktop a class or an object?

I'm thinking about making a process list array that holds all instances of objects. It would hold an instance of desktop. Does this make sense? Or should I just have one global class called desktop that I don't instantiate?

+5  A: 

I'm wondering whether I should make the desktop a class or an object

That's an easy decision, as in JavaScript there are no classes -- just objects.

JavaScript is a prototype-based language and not class-based.

You may want to check the following Stack Overflow posts for further reading on the topic:

Daniel Vassallo
+3  A: 

JavaScript doesn't have classes, only objects. You can chose how to initialize that object, either as a singleton (var desktop = {};) or with a constructor (var desktop = new Desktop();).

I usually make a singleton object, because there is no point in making the constructor if you are only ever going to construct it once. I know others like to make an anonymous self executing function (var desktop = (function(){return {}; })();), but it's pretty much the same thing.

Marius
So, do you think I should instantiate an object (new Desktop()) rather than having a static singleton object?
rFactor
no, have a singleton object with an init function.
Marius
Is there a particular reason as to why this would be a better option?
rFactor