Custom setters and getters
const User = attributes({
firstName: String,
lastName: String,
age: Number,
})(
class User {
get firstName() {
return `-> ${this.get('firstName')}`;
}
set lastName(newLastname) {
return this.set('lastName', `Mac${newLastName}`);
}
get age() {
// do NOT do that. Instead, use this.get and this.set inside getters and setters
return this.age * 1000;
}
// this is NOT an attribute, just a normal getter
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
);
const user = new User({ firstName: 'Connor', lastName: 'Leod' });
user.firstName; // -> Connor
user.lastName; // MacLeod
user.fullName; // -> Connor MacLeodInheritance
Last updated