我们可以设置持久的默认参数,在明确更改之前保持设置吗? [英] Can we set persistent default parameters which remain set until explicitly changed?

查看:14
本文介绍了我们可以设置持久的默认参数,在明确更改之前保持设置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个函数 fn,其中预期结果是 a, b, c 定义在每次调用 fn,无论是否传递对象参数.如果传递设置属性的对象,则应仅为该对象设置属性.

const fn = (opts = {a:1, b:2, c:3}) =>控制台日志(选项);

不带参数调用时,结果为

fn()//{a: 1, b: 2, c: 3}

带参数调用时,例如{b:7},预期结果为

fn({b:7})//{a: 1, b: 7, c: 3}

然而,实际结果是

fn({b:7})//{b: 7}

通过在函数外定义一个对象并在函数体内使用Object.assign(),能够得到预期的结果

const settings = {a: 1, b: 2, c: 3};const fn = opts =>{opts = Object.assign({}, settings, opts);控制台日志(选项)}fn({b: 7})//{a: 1, b: 7, c: 3}fn();//{a: 1, b: 2, c: 3}/*//不记录错误;不返回预期结果const fn = (opts = Object.assign({}, settings, opts)) =>控制台日志(选择)*/

是否可以仅使用默认参数来实现上述结果,而无需在函数参数之外或函数体内定义要引用的对象?

解决方案

也许我误解了这个问题,但您似乎正在寻找每个单独属性的默认初始化程序.为此,您必须使用解构:

const fn = ({a = 1, b = 2, c = 3} = {}) =>console.log({a, b, c});

如果您想保留任意属性,而不仅仅是那些您预先知道的属性,您可能会对 对象休息/传播属性建议允许你写

const fn = ({a = 1, b = 2, c = 3, ...opts} = {}) =>console.log({a, b, c, ...opts});

<小时><块引用>

opts 变量作为单个对象引用是否可以仅使用默认参数来实现,而无需定义在函数参数之外或函数体内引用的对象?

没有.参数声明只能使用(部分)参数初始化变量,并且可能(作为语法糖)在没有或 undefined 参数(部分)被传递时使用默认值初始化.它们无法执行无条件计算并创建根据结果初始化的变量 - 这就是您在此处尝试实现的目标.

您应该为此使用函数体.

The below is a function fn where expected result is for a, b, c to defined at every call of fn, whether an object parameter is passed or not. If object is passed which sets property, property should be set only for that object.

const fn = (opts = {a:1, b:2, c:3}) => console.log(opts);

when called without parameters the result is

fn() // {a: 1, b: 2, c: 3}

when called with parameter, for example {b:7}, the expected result is

fn({b:7}) // {a: 1, b: 7, c: 3}

however, the actual result is

fn({b:7}) // {b: 7}

Was able to get expected result by defining an object outside of function and using Object.assign() within function body

const settings = {a: 1, b: 2, c: 3};
const fn = opts => {opts = Object.assign({}, settings, opts); console.log(opts)}
fn({b: 7}) // {a: 1, b: 7, c: 3}
fn(); // {a: 1, b: 2, c: 3}
/*
  // does not log error; does not return expected result
  const fn = (opts = Object.assign({}, settings, opts)) => console.log(opts)
 
*/

Can the above result be achieved solely utilizing default parameters, without defining an object to reference outside of function parameters or within function body?

解决方案

Maybe I misunderstood the question, but you seem to be looking for default initialisers for each separate property. For that, you have to use destructuring:

const fn = ({a = 1, b = 2, c = 3} = {}) => console.log({a, b, c});

If you want to keep arbitrary properties, not just those that you know of up front, you might be interested in the object rest/spread properties proposal that allows you to write

const fn = ({a = 1, b = 2, c = 3, ...opts} = {}) => console.log({a, b, c, ...opts});


Can an opts variable as the single object reference be achieved solely utilizing default parameters, without defining an object to reference outside of function parameters or within function body?

No. Parameter declarations are only able to initialise variables with (parts of) the arguments, and possibly (as syntactic sugar) with default values when no or undefined argument (parts) are passed. They are not able to carry out unconditional computations and create variables inialised from the results - which is what you attempt to achieve here.

You are supposed to use the function body for that.

这篇关于我们可以设置持久的默认参数,在明确更改之前保持设置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆