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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay