简体   繁体   English

在Javascript中模拟子类 - 检查我是如何做到的?

[英]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我正在修补
  • I add some getters email , aliasText , and online that return some values. 我添加一些getters emailaliasTextonline返回一些值。
  • I use this class via 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)它将返回contactemailaliasTextonline性能的补丁(如干将虽然)。

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.

相关问题 正如我在代码中所做的那样,在 javascript 上传后,如何上传多个图像文件并相应地查看它们? - How can I upload multiple image file and view them accordingly after upload by javascript as I've done in the code? 我有一个javascript函数,我无法分辨自己做错了什么 - I have a javascript function, and I can't tell what I've done wrong 如何检查javascript函数是否已准备好/已完成(jQuery) - How to check if javascript function is ready/done (jQuery) 我想推迟对javascript的解析,我做了一些修改,但是如何检查它们是否正常工作 - I want to defer parsing of javascript, i have done some modifications but how to i check if they are working or not 正则表达式 - 我做错了什么吗? - Regex - is there something I've done wrong? 如何将这两个按钮设置为一个,如果不使用则如何旋转(假设我已正确完成操作) - How do I make these two buttons one and the rotation if off (presuming I've done it correctly) 如何在像我所做的那样排序时显示产品中的所有数据? - How do i display all the data in products while sorting like I've done? (javascript)如何获取我选择的元素的ID? - (javascript) how to i get the ID of an element i've selected? Javascript子类化 - Javascript subclassing 在Javascript中进行子类化 - Subclassing in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM