DEV Community

Discussion on: How you can learn Closures in JavaScript and understand when to use them

Collapse
 
naskkotech profile image
Olohundare Nasirudeen • Edited

You could use .bind function in replace of apply

const baseUrl = 'http://localhost:3000';

  const getEndpoint = (baseUrl, resource, id) => {
      return `${baseUrl}/${resource}/${id ? id: ''}`;
  }

const withBase = getEndpoint.bind(null, baseUrl);
const productsEndpoint = withBase('products')
const productsDetailEndpoint = withBase('products', 1)