SOURCE

/**
 * 文档:https://juejin.cn/post/7300151966964613132
 */
function constructURL(param) {
  const { category, limit, userId } = param;
  const baseURL = "https://fakestoreapi.com/products";
  const url = new URL(baseURL);
  const params = new URLSearchParams();

console.log(url);
  if (category) url.pathname += `/category/${category}`;
  if (limit) params.append('limit', Number(limit).toString());
  if (userId) params.append('userId', Number(userId).toString());

  url.search = params.toString();
  return url.toString();
}

let url = constructURL({category:1,limit:2,userId:3});
console.log(url);
console 命令行工具 X clear

                    
>
console