0

As we know, we can pass to function some arguments and then get access to them through arguments, like this:

function test() {
  return arguments.length;
}

console.log(test('param1', 'param2', 'param3'));

But when the function is an object property, I collided with unexpected behavior for me: I can't get access to arguments inside this function. Here is an example:

const obj = {
    test: () => {
        if (arguments.length === 1) {
            return true;
        } else {
            return false;
        }
    }
};

console.log(obj.test('param1'));

My question is: Why? And is there any other way to get access to passed parameters to this function? (notice, that my functions doesn't accept any arguments in both cases, but in first one I can read arguments, but in second I can't).

Commercial Suicide
  • 14,875
  • 14
  • 62
  • 80
  • Did you look at any documentation on [Arrow Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_binding_of_arguments)? – Patrick Evans Sep 24 '17 at 20:16
  • Running on jsfiddle in chrome prints `true` -> https://jsfiddle.net/aba5x9u0/ – Jankapunkt Sep 24 '17 at 20:17
  • 2
    This has nothing to do with the function being nested in the object, it does have to do with it using arrow syntax instead of being a `function`. – Bergi Sep 24 '17 at 20:17
  • Read this OP: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions, it's mentioned in the definition. – doubleOrt Sep 24 '17 at 20:18
  • 1
    @Bergi Doesn't SO turn into some sort of documentation site by closing these sorts of questions ? I mean if someone runs into the same problem as OP, they will normally google something like "Can't read “arguments” from function nested in object" instead of "Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?" – doubleOrt Sep 24 '17 at 20:20
  • 1
    @Jankapunkt, that is because JSFiddle by default runs the code inside another function (window.onload) thus the arguments object it sees is the one provided by the outer window.onload scope – Patrick Evans Sep 24 '17 at 20:20
  • @Pareick Evans great knowledge, good to know for using fiddle – Jankapunkt Sep 24 '17 at 20:21
  • 1
    @Taurus I don't think there's anything wrong with that, it's one of the goals of SO. And yes, if they google that, they will find this question here and get redirected to one that holds the answer. – Bergi Sep 24 '17 at 20:22
  • @Bergi correct, one thing still remains though: the asker will have to read a long answer with parts that are very unrelated to their question until they get to the part of the answer they need (after they're redirected to that answer). To some, reading the long answer may be easy, but I like reading long things on paper rather than on a screen, so... – doubleOrt Sep 24 '17 at 20:27
  • 1
    @Taurus, *"they will normally google something like 'Can't read “arguments” from function nested in object' instead of 'Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?'"*. You're totally right! – Commercial Suicide Sep 25 '17 at 14:27

0 Answers0