简体   繁体   English

在不重新分配命名参数的情况下调用函数

[英]Calling a function without reassigning named argument

I have a function that has named arguments. 我有一个命名参数的函数。

I am trying to call the same function from different events without overwriting the values of the others previously assigned. 我试图从不同的事件调用相同的函数,而不覆盖以前分配的其他事件的值。

I have tried store the values of the previously clicked buttons in variables but that doesn't work. 我已经尝试将先前单击的按钮的值存储在变量中,但这不起作用。

Is there a way to call the functions and assign a single argument at a time without overwriting the others? 有没有办法调用函数并一次分配一个参数而不覆盖其他参数?

 function mainFun({ val1, val2, val3 }) { var value1 = val1, value2 = val2, value3 = val3; // console.log(value1, value2, value3); console.log(val1, val2, val3); } 
 <button onclick="mainFun({val1 : 'Caller 1'})">First Event</button> <button onclick="mainFun({val2 : 'Caller 2'})">Second Event</button> <button onclick="mainFun({val3 : 'Caller 3'})">Third Event</button> 

Trying to achieve: 试图实现:

On First Event > Caller 1 undefined undefined 在第一个事件>来电者1未定义未定义

On Second Event > Caller 1 Caller 2 undefined 在第二个事件>来电者1来电2未定义

On Third Event > Caller 1 Caller 2 Caller 3 在第三个事件>来电者1来电者2来电者3

Thanks in advance! 提前致谢!

You could store the values as property of the function. 您可以将值存储为函数的属性。

 function mainFun({ val1 = mainFun.val1, val2 = mainFun.val2, val3 = mainFun.val3 }) { mainFun.val1 = val1; mainFun.val2 = val2; mainFun.val3 = val3; console.log(val1, val2, val3); } 
 <button onclick="mainFun({ val1: 'Caller 1' })">First Event</button> <button onclick="mainFun({ val2: 'Caller 2' })">Second Event</button> <button onclick="mainFun({ val3: 'Caller 3' })">Third Event</button> 

If you do not like to store the values as properties of the function, you could use a closure and return a function for the same group of use cases. 如果您不希望将值存储为函数的属性,则可以使用闭包并为同一组用例返回函数。

 function mainFun(defaults = {}) { return function({ val1 = defaults.val1, val2 = defaults.val2, val3 = defaults.val3 }) { defaults.val1 = val1; defaults.val2 = val2; defaults.val3 = val3; console.log(val1, val2, val3); }; } var work = mainFun(); 
 <button onclick="work({ val1: 'Caller 1' })">First Event</button> <button onclick="work({ val2: 'Caller 2' })">Second Event</button> <button onclick="work({ val3: 'Caller 3' })">Third Event</button> 

value1 , value2 and value3 variables are local to your mainFun function. value1value2value3变量是mainFun函数的本地变量。 They're being garbage collected after each mainFun execution is done. 在每次mainFun执行完成后,它们都被垃圾收集。

To achieve what you want to achieve, you'll need to store the previous parameters somewhere. 要实现您想要实现的目标,您需要将以前的参数存储在某处。

There are many possibilities : 有很多可能性:

  • One of them is global variables : 其中一个是全局变量:

 let value1, value2, value3; function mainFun({ val1, val2, val3 }) { value1 = val1 || value1; value2 = val2 || value2; value3 = val3 || value3; console.log(value1, value2, value3); //console.log(val1, val2, val3); } 
 <button onclick="mainFun({val1 : 'Caller 1'})">First Event</button> <button onclick="mainFun({val2 : 'Caller 2'})">Second Event</button> <button onclick="mainFun({val3 : 'Caller 3'})">Third Event</button> 

  • Another solution would be to store them as the functions properties (as Nina Scholz suggested) 另一种解决方案是将它们存储为函数属性(如Nina Scholz建议的那样)

Please correct me if I'm misunderstanding your question. 如果我误解你的问题,请纠正我。 But if you want to store te values, you'll have to declare them outside of your function. 但是如果你想存储te值,你必须在函数之外声明它们。 Currently your values are only alive within your function. 目前,您的值仅在您的函数中存活。 When the function is called they will be re-created and after the function finishes they will be deleted again. 调用该函数时,将重新创建它们,在函数完成后,它们将再次被删除。

You could try to declare your values outside of your function and prevent overriding like this: 您可以尝试在函数之外声明值,并防止覆盖如下:

var value1, value2, value3;
function mainFun({
  val1,
  val2,
  val3
}) {
   value1 = value1 || val1,
    value2 = value2 || val2,
    value3 = value3 || val3;
  console.log(value1, value2, value3);
}

what the value1 || val1 什么value1 || val1 value1 || val1 basically does is say 'hey, if value1 already has a value, use that one. value1 || val1基本上就是说'嘿,如果value1已经有一个值,那就用它。 If not, use val1 . 如果没有,请使用val1 Acts the same as value1 = value1 !== undefined ? val1 : value1 行为与value1 = value1 !== undefined ? val1 : value1相同value1 = value1 !== undefined ? val1 : value1 value1 = value1 !== undefined ? val1 : value1 or easier: value1 = value1 !== undefined ? val1 : value1或更容易:

if (value1 === undefined) {
   value1 = val1;
} else { // obviously not needed
   value1 = value1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM