0

How to access function's parameter in another function that invoke it, here's what I mean

function one(a){
  b();
}

function b(){
  //'a' is the parameter of function one
  if(a>0){
   //do some stuff
  }
}

//the parameter 'a' of function one goes here to catch an event
element.addEventListener('wheel', one);

Can I access function one parameter in function b?

5
  • That is like, an absolute basic of JavaScript. See function definitions. Commented Mar 14, 2018 at 7:48
  • Do not try it at home function one(a) { b() } function b() { console.log(arguments.callee.caller.arguments[0]) } Commented Mar 14, 2018 at 7:48
  • Yes, indeed, that is absolute basic of JavaScript ! Commented Mar 14, 2018 at 7:51
  • Tip: don't think of it in terms of accessing parameters of a higher-up caller. You have no idea where your function will be called from. Make functions self-contained with explicit parameters and return values. Commented Mar 14, 2018 at 7:51
  • yes, thanks, returning values is indeed very important Commented Mar 14, 2018 at 7:59

2 Answers 2

3

Pass the parameter into b as well

function one(a){
  b(a);
}

function b(a){
  //'a' is the parameter of function one
  if(a>0){
   //do some stuff
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, thank you, my mistake is was i didn't pass the 'a' parameter in function b declaration. Thanks !
Yeah we sometimes miss the most basic of things until someone else looks at our code :)
1

You have to pass a to function b as follows.

function one(a){
  b(a);
}

function b(a){
  //'a' is the parameter of function one
  if(a>0){
   //do some stuff
  }
}

//the parameter 'a' of function one goes here to catch an event
element.addEventListener('wheel', one);

6 Comments

I'd say because this answer duplicates the one given two minutes before. But who knows why someone did this :)
Because your answer is almost the same with the previous answer posted before you
An you think it was intended?
Note that if that was the reason for the downvote, it's wrong/unfair/incorrect/not what we stand for. If an exact duplicate answer was posted half a year later, that's spurious. But 2 minutes later is perfectly within the range of being written simultaneously, and the answer should be judged on its own merit.
Should I think ManosKounelakis copied @YuryTarabanko 's comment, it was 1min later. You should not copy comments guys.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.