Linter - Parsing error: Unexpected token {

Asked

Viewed 209 times

0

The teacher spent some exercises, one of which was to use Git and run a linter to check several code files. Here he gave me in this code but honestly I could not understand nor identify.

    // Você nao pode modificar as linhas de 2 à 5:
    const lib = {
    abc: 123,
    def: 234,
    };

    object =abc {
      def: lib.def,
      abc: lib.abc,
      xyz: 567,
    };

    console.log('O valor de abc est' + object.abc);
  • 1

    object = abc was to be an object with properties def, abc and xyz?

  • I believe so. Now I don’t understand why Parsing error selecting token {

  • but note that if object = abc was to be an Object which variable name object or abc? change to const object and call the object.abc should already work

  • Thanks for the tip

2 answers

2


The error is syntax, more specifically in this part:

object =abc {
  def: lib.def,
  abc: lib.abc,
  xyz: 567,
};

the correct would be:

object = {
  def: lib.def,
  abc: lib.abc,
  xyz: 567,
};

For you are assigning an object to the variable object, and the "abc" has not felt where it was.

  • may have felt if you put the abc in quotes, so calls the object["abc"]

  • @Pedropinto, is one of the possible interpretations

  • const lib = { abc: 123, def: 234, }; const Object { def: lib.def, abc: lib.abc, Xyz: 567, }; console.log('The value of abc est' + Object); and now the error in the linter changes to: Unexpected string concatenation prefer-template (for the last line)

  • @This comma at the end after 567 doesn’t help either.

0

linter recommends using a novelty : ES2015 (ES6) named "Littéraux de gabarits" (in French) or "template literals". https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

It is then possible to format a line of text by directly typing the name of the variable 'inline'.

To solve the problem, one must change

    console.log('O valor de abc est' + object);`

for

    console.log(`O valor de abc est ${object}`);
  • Hi Marcelo! This answer is not the answer to the problem. The problem was how others indicated the wrong syntax in the object declaration. Template literals is useful, but if you don’t fix the syntax object =abc { will make the same mistake anyway. I think Samir’s reply deserves to be marked as accepted/correct.

  • Hi @Sergio .. is that after using what our friend Samir wrote there was another error for Unexpected string concatenation prefer-template (for the last line) and the answer was this one I passed. Sorry for posting in the wrong place ;)

  • The error-free code looks like this: // Vous ne pouvez pas Modifier les Lignes 2 à 5: const lib = { abc: 123, def: 234, }; const Object = { def: lib.def, abc: lib.abc, Xyz: 567, }; const str = La valeur de abc est ${object}; console.log(str);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.