0

Is it possible to access arguments like this?

 let a = 0;
 let b = 1;

 if (a === 0 || b === 1) {
      let args = this.arguments; // placeholder -> this is what this question is about

      console.log(`${} is equal to 0 and ${} is equal to 1!`);
      // expected output: a is equal to 0 and b is equal to 1!
 }

Subsequently, with the same function, would it be possible to do something like:

 let a = 0;
 let b = 1;

 if (a === 0 || b === 1) {
      let args = this.arguments; // placeholder -> this is what this question is about

      console.log(`${} is equal to ${} and ${} is equal to ${}!`);
      // expected output: a is equal to 0 and b is equal to 1!
 }
Mike K
  • 7,621
  • 14
  • 60
  • 120
  • 2
    In javascript, only functions have `arguments`. If you're looking for something like python's `locals()`, there's no such thing. – georg Jun 22 '19 at 19:09
  • 1
    Please differentiate the code blocks - they are exactly the same. Can you clarify? – Randy Casburn Jun 22 '19 at 19:17
  • The difference is the first code block accesses only the variable, whereas the second accesses the variable and the value to compare it against – Mike K Jun 22 '19 at 19:18
  • 2
    Got it - thanks! Wow that was really obscure. But anyway, your question doesn't make sense as written. The (now deleted) answer was addressing the question in your title - which is completely different than your question in your code. Please consider changing the title of this question so it does not reflect **_in a function call**_ as that is misleading. – Randy Casburn Jun 22 '19 at 19:24
  • @georg well not exactly, `this` in the global scope is pretty much equivalent to `window` (in the browser) or `globals` (in Node). See [this question](https://stackoverflow.com/questions/39960/javascript-equivalent-of-pythons-locals) for more details. – Nino Filiu Jun 22 '19 at 19:27
  • 1
    BTW [the question I mentionned](https://stackoverflow.com/questions/39960/javascript-equivalent-of-pythons-locals) seems to be a duplicate - can't be sure though, as the OP's question is very unclear. – Nino Filiu Jun 22 '19 at 19:28
  • Thanks everyone, I've also updated the title – Mike K Jun 22 '19 at 19:30
  • 1
    You can already access `a` and `b` as they are in scope, so what is it that you need to do? – Ace Jun 22 '19 at 21:12
  • You would simply write `console.log(`a is equal to ${a} and b is equal to ${b}!`);`. An `if` statement is not a function call, it does not have "arguments". – Bergi Jun 22 '19 at 21:59

0 Answers0