Take the requirements for webRTC usage as an example

// in vue's methods
ready(){
    const pc = new RTCPeerConnection(iceServers)
    // You want to mount userId on the instance
    pc.userId = userInfo.userId
    pc.ontrack = thisOnTrack},onTrack(event) {
  this.getClient(event.target.userId).localStream = event.streams[0];
},

// Get the video data of the corresponding userId
getClient(userId) {
  return this.clients.find((o) = > o.userId === userId);
},
Copy the code

OnTrack is a callback method that calls onTrack after a successful RTC connection. This write normal is no problem, but, encounter to be compatible with ios. If the event. Target does not contain a userId, it will return undefined. Because the callback is not the method I call, the parameter is someone else I can not directly add the parameter, what to do?

Work out (eat out)

// Arrow function, where declared, where the context (scope)
pc.ontrack = (e) = > {
    this.onTrack(e, userInfo.userId)
};
onTrack(The event, userId) {
  this.getClient(userId).localStream = event.streams[0];
},
Copy the code

In a kind of closure, the function calls the function, which is declared where the arrow function context (scope) is, and calls the function internally to add any arguments it wants to pass in

Ps: Actually a simple question. Not at first. Then make a complex note, red-hot trance…. (The first Nuggets article)