Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript function that returns an object

This function is supposed to return an object, however, the construction used below is unfamiliar to me. How does this function work?

function expect(value) {
  return {
    toBe: exp => console.log(success)
  }
}
like image 905
Gleb Krylov Avatar asked Feb 07 '26 07:02

Gleb Krylov


1 Answers

This is a standard JavaScript function:

function(parameter1, parameter2) {
    return returnVal;
}

But the object that is being returned looks like this:

{
    toBe: exp => console.log(success)
}

Which is an object containing an ES6 arrow function, and can be alternatively expressed like so (a direct translation to an ES5 function):

{
    toBe: function(exp) { 
        return console.log(success);
    }
}

Read here for more information on ES6 arrow function notation.

like image 58
Jack Bashford Avatar answered Feb 09 '26 01:02

Jack Bashford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!