1

I have a bunch of classes that are called with varying amounts of arguments:

class Square {
    height;
    width;
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

Instances of these classes are stored in an array to keep a timeline of them:

const sq = new Square(22, 23);
const history = [sq];

I would like to find a way of getting the arguments from the classes constructor, this is as far as I got:

history[0].contructor.arguments 

Which results in an error:

VM448:1 Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at :1:24

I want to get the contents of the arguments keyword from the constructor (so the array like object that has both the height and width arguments).

I realise I could use a super class and extend it in all of the classes I need this functionality from, but ideally would like to avoid modifying them. Any ideas?

6
  • 1
    What do you mean by "get the arguments from the constructor"? Do you want to check how many parameters it takes, or what was it called with? The latter shouldn't be possible. Commented Oct 17, 2019 at 11:01
  • 1
    If you want to check what arguments the constructor got, why not simply store them on the constructed object? Something like this._arguments = [...arguments]? Commented Oct 17, 2019 at 11:02
  • may be relevant? Commented Oct 17, 2019 at 11:06
  • @VLAZ I was hoping the instance may have it stored in some secret dunder property. Commented Oct 17, 2019 at 11:27
  • @somethinghere Yea I wanted to avoid that so I didn't have to modify a bunch of code but looks like it may be the only way Commented Oct 17, 2019 at 11:27

1 Answer 1

2

The arguments object is locally scoped to the function it belongs to. It is not accessible outside that function unless you pass it from that function elsewhere.

If you want to know what arguments were passed to the constructor from somewhere else, then you need to either pass them explicitly or have the constructor store them somewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping there may be a property on the instance they'd be sat it. Thanks for clearing that up.

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.