zxCoder
V2EX  ›  问与答

关于 js 面向对象

  •  
  •   zxCoder · Nov 29, 2020 · 1265 views
    This topic created in 2079 days ago, the information mentioned may be changed or developed.

    有个小任务,需要实现一个 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 replies    2020-11-29 22:16:44 +08:00
    alphat
        1
    alphat  
       Nov 29, 2020
    class
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5311 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:57 · PVG 16:57 · LAX 01:57 · JFK 04:57
    ♥ Do have faith in what you're doing.