Rob's garden / notes / note

Note

#javascript

They’re are revisiting protocols in @TC39! Could be very powerful and make more composition based patterns viable.

It’s not really like TypeScript interfaces but more like Swift’s implementation, all built on the mysterious internal Symbol. It’s kind of like the syntax sugar for classes but for the Symbol logic that’s used a lot under the hood in JavaScript.

You create a protocol and apply it to classes or objects then those things can share implemented features.

protocol Greet {
  requires name;

hello() {
    console.log(`Hey, ${this[Greet.name]}`)
  }
}

let obj = { [Greet.name]: "Geoff" }
Protocol.implement(obj, Greet)

obj.hello()

(I think this is how it works)