-1

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

2 Answers2

1

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");
tkausl
  • 13,686
  • 2
  • 33
  • 50
  • I used other way: ` let fn = (() => { let ab = {}; let register = () => { for(let key in ab){ console.log("hello"+ab[key]); } }; return (x,y) => { ab[x]=y; return register(); }; })(); ` – user11098422 Feb 22 '19 at 15:17
0

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();
    };
})();
Sean
  • 100
  • 7