Unable to understand function parameter signature for the getMovies function of moviesDAO.js. Can anyone please simplify the parameter passed to this function

static async getMovies({
    // here's where the default parameters are set for the getMovies method

    filters = null,

    page = 0,

     moviesPerPage = 20,

     } = {}) {

         let queryParams = {}
        .
        .
        .
}

My guess is that getMovies() has 1 optional parameter. Which is an object with 3 fields. Each field having a different value. So calling getMovies() without any parameter would me get me the first 20 unfiltered movies of page 0. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters

1 Like

Thanks for the reply. But why an empty object is assigned to the default parameter?

If not you could not call getMovies() like this, without any parameter. With the given signature you can call getMovies in the following ways:

  1. getMovies() which is equivalent to getMovies({}) which is equivalent to getMovies( { filters : null, page : 0, moviesPerPage : 20 } )
  2. getMovies( { page : 2 } ) which is equivalent to getMovies( { filters : null, page : 2, moviesPerPage : 20 } )
  3. getMovies( { moviesPerPage : 16 } ) which is equivalent to getMovies( { filters : null, page : 0, moviesPerPage : 16 } )
  4. getMovies( { page : 2 , moviesPerPage : 16 } ) which is equivalent to getMovies( { filters : null, page : 2, moviesPerPage : 16 } )
  5. and many more

I am sure you get the pattern. This is standard default parameters usage.

1 Like

Thanks, @steevej for the explanation. So if I am getting this right, it means to call getMovies() method without or any number of arguments we are assigning an empty object in the parameter signature.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.