我有一个函数,该函数具有存储在对象中的默认配置。该函数带有可选参数“ options”,如果options参数中有任何配置,则应覆盖默认配置。
这是我的实现:
const myFunction = (props) => {
const config = {
slopeModifier: 2,
minDistance: 30,
lockScrollIfHorizontal: true,
callback: null,
};
if (props.options) {
for (const property in options) {
config[property] = options[property];
}
}
};
export default myFunction;
在其他文件中,我将执行以下操作:
import myFunction
const options = {minDistance: 50, slope: 3};
myFunction({arg1, arg2, options});
此解决方案是一种好的做法吗?有没有一种标准的方法如何存储默认值并用可选参数覆盖它们?另外,我收到了[[eslint(guard-for-in)警告,我的代码可能会引起任何错误吗?