Javascript的Class类实践

JiHua
2023-06-29
43

Javascript的Class类实践

// 复用传递参数
class PutDataClass {
  cagUserName: string = ''
  articleId: string = ''
  func: string = ''
}

相对而言像这样的类实例 在面对需要重复传参的函数来说不用多次重复定义

使用效果

// 删除留言
const deleteComment = async (username: string, commentId: number | string) => {
  if (await useELTips.WarningTips('删除这条评论')) {
    const delCommentPutData = new PutDataClass();
    delCommentPutData.cagUserName = username
    delCommentPutData.articleId = String(commentId)
    delCommentPutData.func = 'delComment'
    const { data: res } = await ArticleRequest.cagUPData(delCommentPutData)
    if (res.status === 200) {
      getArticle()
    }
  }
}
// 选择文章状态并改变
const selectAction = async (selectValue: string) => {
  if (await useELTips.WarningTips('改变文章状态')) {
    const selectActionPutData = new PutDataClass();
    selectActionPutData.cagUserName = ArticleData.data.article.username
    selectActionPutData.articleId = props.ArticleId
    selectActionPutData.func = selectValue
    await ArticleRequest.cagUPData(selectActionPutData)
  }
}
// 删除该文章
const deleteArticle = async (articleId: string | number) => {
  const delArticlePutData = new PutDataClass();
  delArticlePutData.cagUserName = ArticleData.data.article.username
  delArticlePutData.articleId = String(articleId)
  delArticlePutData.func = 'delArticle'
  await ArticleRequest.cagUPData(delArticlePutData)
}
PS:好像可以连这三个函数一起封装进Class里唉!不确定,再看看
标签:Javascript的Class类实践