olabie
olabie

Reputation: 24

How can arrow function's access it's parameters value?

Well while studying how JavaScript behind the scenes work I read that arrow functions does not have their own 'this' keyword nor their own parameters.

//MAIN PROBLEM However, you can access the arguments without any problem. (when I saw the 'this' behavior then I assumed it's gonna be the same for the arguments and so the arguments value must be the value of the arguments of the parent function.)

Please someone explain how this is possible ???? that arrow functions could still access it's arguments even though "Arrow functions don't have their own arguments".

I checked it in code and it was true for the 'this' keyword when used inside the arrow function the value of 'this' is going to be the 'this' from it's parent function.

Upvotes: -2

Views: 156

Answers (1)

Tachibana Shin
Tachibana Shin

Reputation: 3908

Arrow functions don't have their own arguments Allusion to arguments which is a legal and weird inheritance of JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

function helpNor() {
  console.log("arguments normal function: ", arguments)
}

const helpArr = () => {
  if (typeof arguments !== "undefined")
    console.log("arguments arrow function: ", arguments)
  else
    console.log("arguments arrow function not exists")
}

helpNor("ls", "-a")
helpArr("ls", "-a")

However this does not mean that the arrow function does not have arguments

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

const help = (...args) => {
  console.log("arguments: ", args)
}

help("ls", "-a")

Upvotes: -1

Related Questions