V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
zxCoder
V2EX  ›  问与答

关于 js 面向对象

  •  
  •   zxCoder · 2020-11-29 21:42:55 +08:00 · 751 次点击
    这是一个创建于 1215 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有个小任务,需要实现一个 mongodb 客户端的一个简单代理,要用 js 语言

    快速了解了一下 js 的面向对象机制之后,写下了这样的代码。。。。因为没时间去深入了解,想请问下有更方便的写法吗,就是我希望除了指定的一些函数需要重新写,其他的函数都能够默认去调用原生的 MongoClient 的对应函数

    function ProxyClient(username,password,host){
        this.client=new mongodb.MongoClient(
            util.format("mongodb://%s:%s@%s",username,password,host),
            { useUnifiedTopology: true });
    }
    ProxyClient.prototype.connect=function (){
        return this.client.connect();
    }
    ProxyClient.prototype.db=function (){
        return this.client.db();
    }
    ProxyClient.prototype.close=function (){
        return this.client.close();
    }
    ......
    let proxyClient=new ProxyClient("user","pass","localhost");
    run(proxyClient).catch(console.dir);
    
    2 条回复    2020-11-29 22:16:44 +08:00
    alphat
        1
    alphat  
       2020-11-29 21:47:35 +08:00
    class
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3587 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:44 · PVG 12:44 · LAX 21:44 · JFK 00:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.