函数参数中的默认对象与默认属性:如何避免默认值重复定义?
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:
1. Extract Defaults to a Reusable Object (Most Recommended)
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(),optionsdefaults to an empty object, and merging it withDEFAULT_OPTIONSgives 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 inDEFAULT_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 ofundefined(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

