Help Understanding Syntax (Call Method)

I shaved much of the code down to focus on the main part I need help understanding.

The point of this code is to use the call method to run the display Results method.

The call method, takes in the answers parameter for the this and then passes in a array. The syntax for this throw’s me off, because all examples of this I have seen have only ever had an array passed for the call method but never quite like this.

Could someone please help me to understand, why is the answers parameter passed within curly braces sounding it as well as the array ?

I don’t fully understand why it is passed in this way, and I see it is marked as an error if passed without them.

I would greatly appreciate any assistance on this.

const poll = {

  answers: new Array(4).fill(0),

 displayResults (type = 'array') {
    //The Error is around the if and else statements. the codes where they run are correct

      type === 'string'
        ? `Poll results are ${this.answers.join(', ')}.`
        : `${this.answers.join(', ')}`
};{ answers: [5, 2, 3] }, 'string');{ answers: [1, 5, 3, 9, 6, 1] }, 'string');

I don’t know if I have understood your problem, but if the problem are the curly braces around the answers parameter, that’s because the first parameter of .call() must be an object:, arg1, arg2, ...)

So in your case the curly braces create an object literal. It the same thing as doing this:

const  array_to_print = {
    answers : [5, 2, 3]
};, 'string');

About the cases you’ve seen when they only pass an array as the first parameter, that’s must create some error, because although the array is an object, the displayResult will try to access to the answers parameter that doesn’t exist in the array object. So I don’t know if you’ve seen them using with this function or others, but in this function as you write it, that must create an error.
I don’t know if I’ve been clear, english is not my first language but I wanna help, let me know

1 Like

Thank you, I was mistaken I confused the call with the apply method.

This type of logic is a bit lost on me. So I understand you when you say the call must accept an object, so by putting curly braces around the answers function, that turns it into a literal object. And we can then pass it with an array ?

I don’t think I’ve ever seen the call method used in such a way, even when I look up examples of it, this is a very new application or at least it does not appear to be a common one so I’m a bit thrown off by it.

Sorry I don’t understand your question, maybe it’s my language gap. But maybe I can help you by saying that the apply and call method works in the same way, the only difference is that the apply method takes the arguments as an array, while with call you can pass all of the also without an array that collect them:, arg1, arg2,/*......*/, argN);
function(thisArg, argsArray)

So as you can see call takes more arguments as parameters, while apply only two. This means that if you’ve already used apply, the call works the same, just with this little difference.
Than you call also pass null as thisArg, and they will just take the global object as object, but if you wanna look closer on this two function here some link:
a) Call() method by MDN documentation
b)Apply() method by MDN documentation
or some simplier explanation:
1)Call() method by W3 school
2) Apply() method by W3 school
Let me know if this helps you