想把JS的匿名函数改为普通函数,求助如何传递参数

现在有类似这样的代码

mui(document).on("tap", ".delete", function() {
    console.log('mui(document).on("tap", ".delete")');
    commentId = this.getAttribute('comment-id');
    console.log(commentId);
    deleteLetterComment(commentId);
});

我想把这里面的function提出来复用。我知道可以这么写

mui(document).on("tap", ".delete", beforeDeleteLetterComment);

function beforeDeleteLetterComment() {
    console.log('mui(document).on("tap", ".delete")');
    commentId = this.getAttribute('comment-id');
    console.log(commentId);
    deleteLetterComment(commentId);
}

然而还有一段代码需要传递参数

function beforesubmitComment(str)
{

}

我想也这样用,但是直接写mui(document).on("tap",".delete",beforesubmitComment(str));不生效

求助该怎么写。

阅读 4k
4 个回答
mui(document).on("tap",".delete",function(){
    beforesubmitComment(str)
});

mui(document).on("tap",".delete",function() { beforesubmitComment(str); });

能想到的方法就是上面两位写的了,其他方法暂时不晓得

判断argument的长度,有就拿来用,没有就是默认空参

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题