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

Javascript 如何为ES6类属性提供默认值?,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,我有一个JavaScript类,我想使用对象提供默认值。如果没有为某些值提供用户输入,我只希望默认值成为类的一部分。然而,我不知道如何实施这一点。这是我的班级: // Class definition, properties, and methods class iTunesClient { constructor(options) { this.term = options.terms; this.country = options.country; this.me

我有一个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: '',
};
// 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()
将传入的值与默认值合并:

// 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: '',
};
// 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()
的工作原理,请执行以下操作:

  • 它以
    {}
    作为目标(空对象)开始
  • 然后将所有默认值复制到空对象中
  • 然后它将所有传入的属性复制到同一个目标中
  • 然后它返回用于初始化所有实例数据的目标对象

  • 当然,如果您的实例属性名称与options对象中的名称相同,您可以这样做:

    // 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];
        });
      }
    }
    
    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;
      }
    }
    

    你可以这样做:

    // 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];
        });
      }
    }
    
    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;
      }
    }
    

    但您必须警惕“虚假”值,例如,如果
    opts.limit
    作为
    0
    传入,则
    此.limit
    将设置为
    defaults.limit
    值,即使opt已定义。

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

    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;
      }
    }
    

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

    我认为更好的解决方案


    我认为,如果您只使用
    |

    阅读起来更清晰,问题代码有什么问题?通常,您会使用
    Object.assign()
    将默认选项与传入的选项合并到一个新对象中,然后使用该对象分配所有值。这是否回答了您的问题?请标记为重复,而不是链接/解释现有答案。