You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

函数参数中的默认对象与默认属性:如何避免默认值重复定义?

Avoid Duplicating Default Values in Object Destructuring for Function Parameters

Great question! Having to repeat default values both in the destructuring pattern and the default parameter object is definitely a pain—especially when dealing with complex object structures. Here are a couple of clean, maintainable ways to eliminate that repetition:

The simplest approach is to define your default values in a single object, then merge it with the incoming parameters inside the function. This way, you only write your defaults once, and any updates only need to happen in one place.

// Define your default options once (can be inside or outside the function, depending on scope needs)
const DEFAULT_OPTIONS = {
  id: 'na',
  name: 'na',
  // For complex nested structures, this works too:
  address: {
    city: 'unknown',
    zipCode: '00000'
  }
};

function doSomething(options = {}) {
  // Merge the incoming options with defaults—incoming properties override defaults
  const finalOptions = { ...DEFAULT_OPTIONS, ...options };
  // Now you can destructure the merged object, or use finalOptions directly
  const { id, name, address } = finalOptions;

  // Example usage
  console.log(`ID: ${id}, Name: ${name}, City: ${address.city}`);
}

How it works:

  • If no arguments are passed to doSomething(), options defaults to an empty object, and merging it with DEFAULT_OPTIONS gives you all the default values.
  • If you pass partial options (e.g., doSomething({id: '123'})), only the provided properties override the defaults—everything else stays as defined in DEFAULT_OPTIONS.
  • For nested objects, use a deep merge utility (like a custom function or Lodash’s _.merge) if you want to override nested properties without replacing the entire nested object. Shallow merging (via the spread operator) works fine for most simple cases.

2. Use Default Values in Destructuring with a Fallback Empty Object

For simpler, flat object parameters, you can skip repeating the full default object entirely by setting the outer parameter to an empty object as the fallback, then defining property defaults directly in the destructuring pattern.

function doSomething({ id = 'na', name = 'na' } = {}) {
  console.log(id, name);
}

How it works:

  • The = {} ensures that if no argument is passed, we destructure an empty object instead of undefined (which would throw an error). Each property then uses its own defined default value.
  • This keeps the code concise and keeps destructuring visible in the parameter list. The only downside is that for deeply nested objects, it can still get verbose if you need to set defaults for nested properties:
    function doSomething({ 
      id = 'na', 
      name = 'na', 
      address: { city = 'unknown', zipCode = '00000' } = {} 
    } = {}) {
      console.log(id, name, city);
    }
    

Which to Choose?

  • Use method 2 for simple, flat object parameters—it’s concise and keeps destructuring front-and-center.
  • Use method 1 for complex or nested objects, or when you need to reuse default values elsewhere in your codebase. It’s more scalable and easier to maintain over time.

内容的提问来源于stack exchange,提问作者NickW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:57:31