search

Home  >  Q&A  >  body text

node.js - 一段js代码的简写问题


    options.filename = dbPath+'picker';
    pickerDB = new Datastore(options);
    options.filename = dbPath+'data';
    dataDB = new Datastore(options);
    options.filename = dbPath+'web';
    webDB = new Datastore(options);
    options.filename = dbPath+'url';
    urlDB = new Datastore(options);
    options.filename = dbPath+'attach';
    attachDB = new Datastore(options);
    options.filename = dbPath+'cacheUrl';
    cacheUrlDB = new Datastore(options);
    options.filename = dbPath+'cache';
    cacheDB = new Datastore(options);
    options.filename = dbPath+'cron';
    cronDB = new Datastore(options);
    options.filename = dbPath+'log';
    logDB = new Datastore(options);
    options.filename = dbPath+'cronLog';
    cronLogDB = new Datastore(options);

请教一下,这一大段,都是复制粘贴。能用更简洁的代码一次搞定吗?

PHP中文网PHP中文网2867 days ago617

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:59:06

    I don’t know if it counts as the abbreviation in your mind. I think from the perspective of repetition, extract the different parts and make an array, and abstract the same parts, as follows:

    let options = {},
        dbPath = ''; //TBD by yourself
    
    let stores = [
        'picker',
        'data',
        'web',
        'url',
        'attach',
        'cacheUrl',
        'cache',
        'cron',
        'log',
        'cronLog']
        .map(key => (options.filename = dbPath + key, new Datastore(options)));
    
    
    console.log(stores); //stores you want

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:59:06

        /*
            options == ?
            dbPath == ?
        */
        var _Datastore = {};
        _Datastore._opts = options;
        _Datastore._path = dbPath;
        _Datastore.Create = function(pathAddon){
            this._opts.filename = this._path + pathAddon;
            return new Datastore(this.options);
        };
        
        pickerDB = _Datastore.Create('picker');
        dataDB = _Datastore.Create('data');
        webDB = _Datastore.Create('web');
        urlDB = _Datastore.Create('url');
        attachDB = _Datastore.Create('attach');
        cacheUrlDB = _Datastore.Create('cacheUrl');
        cacheDB = _Datastore.Create('cache');
        cronDB = _Datastore.Create('cron');
        logDB = _Datastore.Create('log');
        cronLogDB = _Datastore.Create('cronLog');
    

    I don’t know how to use your next code? If the logic code behind is easy to change, you can change it to:

        var _Datastore = {};
        _Datastore._opts = options;
        _Datastore._path = dbPath;
        _Datastore._cache = {};
        _Datastore.Create = function(pathAddon){
            this._opts.filename = this._path + pathAddon;
            return new Datastore(this.options);
        };
        _Datastore.DB = function(pathAddon){
            this._cache[pathAddon] = this._cache[pathAddon] || this.Create(pathAddon);
            return this._cache[pathAddon];
        };
    
        
        //调用 cronLogDB 时 改成 
        _Datastore.DB('cronLog');
        
        //第一次 会创建,第二次直接用之前创建的实例

    reply
    0
  • Cancelreply