Javascript 将特定参数传递给函数

Javascript 将特定参数传递给函数,javascript,vue.js,vuejs2,javascript-objects,Javascript,Vue.js,Vuejs2,Javascript Objects,我有一个功能: getBooks(page, count, authorId){ return API.get('/books/?page=' +page + '&count='+count+ '&author='+ authorId}) .then(response => { return response.data }) } 我正在实现搜索功能,在搜索中我只传递authorId,即 getBooks(

我有一个功能:

getBooks(page, count, authorId){
      return API.get('/books/?page=' +page + '&count='+count+ '&author='+ authorId})
        .then(response => {
            return response.data
        })
}
我正在实现搜索功能,在搜索中我只传递
authorId
,即

getBooks(akdjh23)

但在执行此操作时,它将
akdjh23
设置为
page
。i、 e
page=akdjh23
但我想要
authorId=akdjh23

我的问题是如何在搜索期间只发送
authorId
,在一般get请求时只发送
page
count

基于id的搜索:

getBooks(id)
获取所有书籍:

get(1, 40) -> page=1, count=40

我们应该做到这一点。请注意,您的代码不会作为页面使用,函数中的计数也不会被定义。

您可以更改getBooks的工作方式并传递一个对象,而不是多个参数

getBooks({ page, count, authorId })
如果需要,现在可以传递authord

getBooks({ authorId: 'authorIdPassed' });

您可以传递一个对象而不是固定的参数,并使用
ES6 object destructuring

例如

getBooks({ page, count, authorId }){
  return API.get('/books/?page=' +page + '&count='+count+ '&author='+ authorId})
    .then(response => {
        return response.data
    })
}

getBooks({ authorId: 45});
getBooks({ page: 'abc', authorId: 67});

您不能在
authorId}
getBooks({ page, count, authorId }){
  return API.get('/books/?page=' +page + '&count='+count+ '&author='+ authorId})
    .then(response => {
        return response.data
    })
}

getBooks({ authorId: 45});
getBooks({ page: 'abc', authorId: 67});