DEV Community

Claudio Fior
Claudio Fior

Posted on

Javascript mixin

I'm an old PHP developer and I like traits.

I have to interact with a bank, the class in Bank that does login and add the traits to get and commit requests.

trait GetRequest {
   public function getRequest() {
      ...
   }
}
trait CommitRequest {
   public function commitRequest($data) {
      ...
   }
}
class Bank {
   use GetRequest;
   use CommitRequest;
   public function login() {
       ...
   } 
}
Enter fullscreen mode Exit fullscreen mode

So I can split class traits in different parts.

I had to work on a Javascript project.
Ahhhh!!! The traits does not exists, what can I do?

Use the mixin


class Bank {
   login() {
       ...
   } 
}
let GetRequest = {
   getRequest() {
      ...
   }
}
let CommitRequest {
   commitRequest($data) {
      ...
   }
}
Object.assign(Bank.prototype, GetRequest);
Object.assign(Bank.prototype, CommitRequest);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)