2

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?

Tower
  • 98,741
  • 129
  • 357
  • 507

2 Answers2

5

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:

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
3

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
  • 57,995
  • 32
  • 132
  • 151
  • So, do you think I should instantiate an object (new Desktop()) rather than having a static singleton object? – Tower Feb 14 '10 at 12:24
  • Is there a particular reason as to why this would be a better option? – Tower Feb 14 '10 at 13:19