[英]Should this JavaScript thing be an object or a singleton class?
I'm working on a JavaScript software that bears resemblance to Windows. 我正在研究一种与Windows相似的JavaScript软件。 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?
或者我应该只有一个名为desktop的全局类,我没有实例化?
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中没有类 - 只是对象。
JavaScript is a prototype-based language and not class-based . JavaScript是一种基于原型的语言,而不是基于类的 。
You may want to check the following Stack Overflow posts for further reading on the topic: 您可能需要查看以下Stack Overflow帖子以进一步阅读该主题:
JavaScript doesn't have classes, only objects. JavaScript没有类,只有对象。 You can chose how to initialize that object, either as a singleton (
var desktop = {};
) or with a constructor ( var desktop = new Desktop();
). 您可以选择如何初始化该对象,可以是单个(
var desktop = {};
),也可以是构造函数( 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. 我知道其他人喜欢做一个匿名的自我执行函数(
var desktop = (function(){return {}; })();
),但它几乎是一样的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.