简体   繁体   English

传递参数以在javascript中起作用

[英]Passing Arguments to function in javascript

I have a function(case) as below, 我有一个函数(情况)如下

let fn = (() => {
  let ab = {}; 
  let register = () => {
    console.log("hello" + ab[x])
  };
  return (x,y) => {
    ab[x] = y;
    return register();
  };
})();

this function is working only when I call as below, 此功能仅在我按以下方式调用时有效,

let x = 'key';
let y = 'value';
fn(x,y);

Is there any chance to call directly like 有没有机会像

fn('key', 'value');

what changes I have to make in function to call directly 我必须对函数进行哪些更改才能直接调用

The problem is your register function doesn't know about x . 问题是您的register函数不了解x You need to pass it through from your previous function: 您需要通过上一个函数传递它:

 let fn = (() => { let ab = {}; let register = (x) => { console.log("hello" + ab[x]) }; return (x,y) => { ab[x] = y; return register(x); }; })(); fn("key", "value"); 

I believe this is because you aren't defining the parameters in the functions. 我相信这是因为您没有在函数中定义参数。

   let fn = ((x,y) => {
  let ab = {}; 
  let register = () => {
    console.log("hello"+ab[x])};
    return (x,y) => {
      ab[x]=y;
      return register();
    };
})();

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

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