js How to add custom parameters to methods with default parameters

For example, websocket needs to call 5 links, but all the processing is the same. It is too uncomfortable to write 5 sets of the same functions
(the last paragraph is the solution, just look at the end in a hurry)

new socket = new Websocket()
socket.onopen = openSocket

new socket2 = new Websocket()
socket2.onopen = openSocket

//调用
socket.onmessage = onMesage;
socket2.onmessage = onMesage2;

//接收
const onMessage(e){
    
    
	conssole.log(e)
}
const onMessage2(e){
    
    
	conssole.log(e)
}

How to solve it at one time by passing in custom parameters? as follows

//调用
socket.onmessage = ((e) => {
    
    
    onmessage(e, '自定义1')
})
socket2.onmessage = ((e) => {
    
    
    onmessage(e, '自定义2')
})

//接收
const onMessage(e,type){
    
    
	conssole.log(e,type)
}

Guess you like

Origin blog.csdn.net/weixin_39423672/article/details/121956855