0

I have a question related to this code:

const somaHorasExtras = (salario, valorHorasExtras) => salario + valorHorasExtras;

const calculaDescontos = (salario, descontos) => salario - descontos;

const verifiqueSe = (valor) => {
  const assercoes = {
    ehExatamenteIgualA(esperado) {
      if (valor !== esperado) {
        // eslint-disable-next-line no-throw-literal
        throw {};
      }
    },
  };
  return assercoes;
};

const teste = (titulo, funcaoDeTeste) => {
  try {
    funcaoDeTeste();
    console.log(`${titulo} passou!`);
  } catch {
    console.error(`${titulo} não passou!!!`);
  }
};

teste('somaHorasExtras', () => {
  const esperado = 2500;
  const retornado = somaHorasExtras(2000, 500);

  verifiqueSe(retornado).ehExatamenteIgualA(esperado);
});

teste('calculaDesconto', () => {
  const esperado = 2300;
  const retornado = calculaDescontos(2500, 200);

  verifiqueSe(retornado).ehExatamenteIgualA(esperado);
});

My question is related to the verifiqueSe function specifically. How does this function work? Does someone can explain how this function work in conjunction with the inner function ehExatamenteIgualA? What is the assercoes which is returned?

Thank you.

6
  • 1
    where is ehExatamenteIgualA declared? Commented Sep 2, 2022 at 21:21
  • obviously it is in a higher scope. Commented Sep 2, 2022 at 21:22
  • assercoes is an object with a function ehExatamenteIgualA Commented Sep 2, 2022 at 21:22
  • 3
    ehExatementeIgualA isn't really an innter function. It's just a member on the assercoes object. That object gets constructed and returned. There's no real interaction between the two except that when you invoke ehEWxatementeIgualA it will have closed over the valor value passed to verifiqueSe when it was created. Commented Sep 2, 2022 at 21:22
  • @FelipeEsteves it's declared on line 7. The { functionName(args) {...} } syntax is shorthand for {functionName: function functionName(args) {...}} Commented Sep 2, 2022 at 21:23

1 Answer 1

2

Your verifiqueSe(valor) function returns an object. You may find it a little confusing, since this syntax:

const foo = {
    bar() {
        //
    }
};

is just a short syntax for object method:

const foo = {
    bar: function () {
        //
    }
};

So in order to call the bar() function, you'd need to reach it through foo:

foo.bar();
Sign up to request clarification or add additional context in comments.

Comments

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.