跳到主要内容

defaults

TS JS Deno

Assigns default values for all properties in an object that are undefined.

Use Object.assign() to create a new empty object and copy the original one to maintain key order, use spread operator ... to combine the default values, finally use obj again to overwrite properties that originally had a value.

typescript
const defaults = (obj: any, ...defs: any[]) =>
Object.assign({}, obj, ...defs, obj);
typescript
defaults({ a: 1 }, { b: 2 }, { a: 3 }); // { a: 1, b: 2 }

// React Components
const props = {
style: {
width: 100,
color: "black",
},
};
let style = defaults(props.style, { height: 200, backgroundColor: "grey" });
assertEquals(style, {
width: 100,
color: "black",
height: 200,
backgroundColor: "grey",
});