如何为ES6类属性提供默认值?

时间:2017-09-29 20:09:00

标签: javascript node.js ecmascript-6

我有一个JavaScript类,我想使用对象提供默认值。如果没有为某些值提供用户输入,我只希望默认值成为类的一部分。但是,我不知道如何实现这一点。这是我的班级:

// Class definition, properties, and methods
class iTunesClient {
  constructor(options) {
    this.term = options.terms;
    this.country = options.country;
    this.media = options.media;
    this.entity = options.entity;
    this.attribute = options.attribute;
    this.callback = options.callback;
    this.limit = options.limit;
    this.lang = options.lang;
    this.version = options.version;
    this.explicit = options.explicit;
    this.url = options.url;
  }
}

以下是我的默认值:

// Default values defined according to iTunes API
const defaults = {
  terms: 'default',
  country: 'US',
  media: 'all',
  entity: '',
  attribute: '',
  callback: '',
  limit: 50,
  lang: 'en-us',
  version: 2,
  explicit: 'yes',
  url: '',
};

我意识到这可以通过default parameters来实现,但我宁愿提供一个包含默认值的对象。

4 个答案:

答案 0 :(得分:6)

执行此操作的典型方法是使用Object.assign()将传入的值与默认值合并:

// Class definition, properties, and methods
class iTunesClient {
  constructor(options) {

    // Default values defined according to iTunes API
    const defaults = {
      terms: 'default',
      country: 'US',
      media: 'all',
      entity: '',
      attribute: '',
      callback: '',
      limit: 50,
      lang: 'en-us',
      version: 2,
      explicit: 'yes',
      url: '',
    };      

    let opts = Object.assign({}, defaults, options);
    this.term = opts.terms;
    this.country = opts.country;
    this.media = opts.media;
    this.entity = opts.entity;
    this.attribute = opts.attribute;
    this.callback = opts.callback;
    this.limit = opts.limit;
    this.lang = opts.lang;
    this.version = opts.version;
    this.explicit = opts.explicit;
    this.url = opts.url;
  }
}

解释Object.assign()如何在这里工作:

  1. {}作为目标(空对象)
  2. 开头
  3. 然后将所有默认值复制到空对象
  4. 然后将所有传入的属性复制到同一目标
  5. 然后它返回用于初始化所有实例数据的目标对象
  6. 当然,如果您的实例属性名称与选项对象中的属性名称相同,则可以采用更干燥的方式执行此操作:

    // Class definition, properties, and methods
    class iTunesClient {
      constructor(options) {
    
        // Default values defined according to iTunes API
        const defaults = {
          terms: 'default',
          country: 'US',
          media: 'all',
          entity: '',
          attribute: '',
          callback: '',
          limit: 50,
          lang: 'en-us',
          version: 2,
          explicit: 'yes',
          url: '',
        };      
    
        let opts = Object.assign({}, defaults, options);
    
        // assign options to instance data (using only property names contained
        //  in defaults object to avoid copying properties we don't want)
        Object.keys(defaults).forEach(prop => {
            this[prop] = opts[prop];
        });
      }
    }
    

答案 1 :(得分:0)

你可以这样做:

class iTunesClient {
  constructor(options) {

    // Default values defined according to iTunes API
    const defaults = {
      terms: 'default',
      country: 'US',
      media: 'all',
      entity: '',
      attribute: '',
      callback: '',
      limit: 50,
      lang: 'en-us',
      version: 2,
      explicit: 'yes',
      url: '',
    };      

    this.term = opts.terms || defaults.terms;
    this.country = opts.country || defaults.country;
    this.media = opts.media || defaults.media;
    this.entity = opts.entity || defaults.entity;
    this.attribute = opts.attribute || defaults.attribute;
    this.callback = opts.callback || defaults.callback;
    this.limit = opts.limit || defaults.limit;
    this.lang = opts.lang || defaults.lang;
    this.version = opts.version || defaults.version;
    this.explicit = opts.explicit || defaults.explicit;
    this.url = opts.url || defaults.url;
  }
}

但你必须警惕' falsy'值,例如如果以opts.limit传入0,则this.limit将设置为defaults.limit值,即使已定义opt。

答案 2 :(得分:0)

我认为,stackoverflow.com/a/48775304/10325885

是更好的解决方案
class User {
    constructor(options = {}) {
        this.name = options.name || "Joe";
        this.age = options.age || 47;
    }
}

如果您仅使用||,我认为它更易于阅读。

答案 3 :(得分:-1)

对于比上述示例更现代的内容,可以使用解构和默认参数。

class iTunesClient {
  constructor({
    term = '',
    country = 'US',
    media = 'all',
    entity = '',
    attribute = '',
    callback = '',
    limit = 50,
    lang = 'en-us,
    version = 2,
    explicit = 'yes',
    url = '',
  }) {
    this.term = terms;
    this.country = country;
    this.media = media;
    this.entity = entity;
    this.attribute = attribute;
    this.callback = callback;
    this.limit = limit;
    this.lang = lang;
    this.version = version;
    this.explicit = explicit;
    this.url = url;
  }
}

这样,所有不属于构造函数对象的参数都将被设置为默认值。您甚至可以传递一个空对象,然后获取所有默认值。