[英]Simulating subclassing in Javascript - check how I've done it?
I'm writing some javascript that's GNOME javascript, which is more-or-less spidermonkey. 我正在写一些javascript,它是GNOME javascript,它或多或少是spidermonkey。 Think of it as spidermonkey code that will never be run outside of spidermonkey, so no need to worry about cross-browser compatibility etc.
可以把它想象成一个永远不会在spidermonkey之外运行的spidermonkey代码,因此无需担心跨浏览器兼容性等。
Basically I have a class Folks.Individual
that is not subclassable, but I want to simulate subclassing it by just patching it, adding a few getters to it. 基本上我有一个类
Folks.Individual
不是可子类化的,但我想模拟子类化它只需修补它,添加一些getters到它。 (For those that are interested, it is a GObject
obtained via gobject introspection and in my version of GJS you can't subclass a GObject
). (对于那些感兴趣的人来说,它是通过gobject内省获得的
GObject
,在我的GJS版本中你不能GObject
)。
Notes: 笔记:
contactSys.get_individual(contactID)
returns the Folks.Individual
I'm patching contactSys.get_individual(contactID)
返回Folks.Individual
我正在修补 email
, aliasText
, and online
that return some values. email
, aliasText
和online
返回一些值。 let contact = new Contact(id);
let contact = new Contact(id);
使用这个类let contact = new Contact(id);
, and simply want to use contact
as if it were a Folks.Individual
, plus the extra properties I patch in. contact
,就像它是一个Folks.Individual
,加上我补丁的额外属性。 If the properties I wanted to patch were not dynamic, I'd just do it like so: 如果我想修补的属性不是动态的,我会这样做:
let contact = contactSys.get_individual(contactID); contact.email = ....; contact.aliasText = ....; contact.online = ....;
I simply want a convenience function about the above such that I can call let contact = new Contact(contactID)
and it will return contact
with the email
, aliasText
and online
properties patched in (as getters though). 我只是想了解一个方便的功能上面,这样我可以打电话
let contact = new Contact(contactID)
它将返回contact
与email
, aliasText
和online
性能的补丁(如干将虽然)。
This is how I've done it at the moment. 这就是我现在这样做的方式。 My question is, is this the "proper" way to do it?
我的问题是,这是“正确”的方式吗? Should I be using
this
instead of contact
within Contact()
? 我应该使用
this
而不是Contact()
的contact
吗? Should I be doing something with Folks.Individual.prototype
instead and calling it a Contact
? 我应该使用
Folks.Individual.prototype
做一些事情并将其称为Contact
吗?
function Contact (contactID) {
let contactSys = Shell.ContactSystem.get_default(),
contact = contactSys.get_individual(contactID); // <-- this is a Folks.Individual
contact._contactSys = contactSys;
/* these are defined as getters in case the underlying values change */
Object.defineProperty(contact, 'email', {get: function () {
return contact._contactSys.get_email_for_display(this);
}});
Object.defineProperty(contact, 'aliasText', {get: function () {
return contact.alias ||
contact.full_name ||
contact.nickname ||
contact.email ||
_("Unknown");
}});
Object.defineProperty(contact, 'online', {get: function () {
return contact.presence_type === Folks.PresenceType.AVAILABLE ||
contact.presence_type === Folks.PresenceType.AWAY ||
contact.presence_type === Folks.PresenceType.EXTENDED_AWAY ||
contact.presence_type === Folks.PresenceType.BUSY;
}});
return contact;
}
Remember - I can't subclass Folks.Individual , so I'll settle for patching instances of it with a wrapper function new Contact(contactID)
so that I can treat it as if it were a subclass. 记住 - 我不能将Folks.Individual子类化 ,所以我会安排使用包装函数
new Contact(contactID)
修补它的实例,以便我可以将它视为子类。 I just want to know if there's a standard way of doing this. 我只是想知道是否有一种标准的方法来做到这一点。
cheers! 干杯!
At the very least you can probably do 'online' a bit more succinctly and efficiently: 至少你可以更简洁有效地“在线”:
var ONLINE = Folks.PresenceType.AVAILABLE |
Folks.PresenceType.AWAY |
Folks.PresenceType.EXTENDED_AWAY |
Folks.PresenceType.BUSY;
Object.defineProperty(contact, 'online', {
get: function(){ return (this.presence_type & ONLINE) > 0 }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.