简体   繁体   中英

Should this JavaScript thing be an object or a singleton class?

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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM