我们可以设置持久的默认参数,这些参数保持设置,直到明确更改 [英] Can we set persistent default parameters which remain set until explicitly changed?
问题描述
以下是 fn
的函数,其中预期结果为 a
, b
, c
在每次调用 fn
时定义,是否通过对象参数。 if / 1,b:2,c:3})=>的console.log(OPTS);
当没有参数调用结果是
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); console.log(opts)} fn({b:7})// {a:1,b:7,c:3} fn(); // {a:1,b:2,c:3} / * //不记录错误;不返回预期结果const fn =(opts = Object.assign({},settings,opts))=> console.log(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});
code> opts 变量作为单个对象引用仅使用默认参数来实现,而不定义要在函数参数之外或函数体内引用的对象?
否。参数声明只能使用参数的(部分)初始化变量,并且可能(当语法糖)默认值为no或未定义
参数(部分)时将其转换为默认值。他们无法执行无条件计算,并从结果中创建变量 - 这是您在此尝试实现的。
您应该使用函数体那个。
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屋!