Trait.js is a helper which allows javascript to have the same behavior with the Php Trait feature. Allows you to inherit or reuse methods to any classes or objects. There's been other libraries/repositories out there that is also respresents the javascript equivalent of Php - Traits. Among them, this solution is a simple and lightweight package and serves only one purpose, allow any classes to inherit methods from multiple traits.
npm install trait.js --save
https://unpkg.com/trait.js@1.2.12/build/trait.min.js
Let's create a simple trait that displays the user's fullname.
import trait from 'trait.js';
export default trait({
getFullname() {
return `${this.firstName} ${this.lastName}`;
},
});
For example we have a user model that wants to use the UserTrait.js
import UserTrait from './UserTrait.js';
export default class User {
constructor({ firstName, lastName }) {
this.firstName = firstName;
this.lastName = lastName;
}
}
UserTrait.inheritTo(User);
// or
UserTrait.in(User); // a shorthand for inheritTo
Let's try to use that method:
import User from './User.js';
const user = new User({
first_name: 'Foo',
last_name: 'Bar',
});
// We can now use the trait method getFullname()
console.log(user.getFullname()); // Foo Bar
Javascript only supports single inheritance: a child class can inherit only from one single parent.
So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.
Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
MIT © 2020 Regs Isabelo