简体   繁体   中英

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 . 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();
    };
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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