class Mail {
constructor(author) {
this.from = author;
this._contacts = [];
}
sendMessage(msg, to) {
console.log(`you send: ${msg} to ${to} from ${this.from}`);
this._contacts.push(to);
}
showAllContacts() {
return this._contacts;
}
}
class WhatsApp extends Mail {
constructor(username, isBussinessAccount, phoneNumber) {
super(phoneNumber);
this.username = username;
this.isBussinessAccount = isBussinessAccount;
}
// Overriding method => Melakukan Override Total
sendMessage(msg, to) {
super.sendMessage(msg,to);
console.log('Send by WA');
}
}
const wa1 = new WhatsApp('dicoding', true, '6281111111');
console.log(wa1.from); // undefined
wa1.sendMessage('halo', '089000999888');
// [1] list of abstractions
const canSendMessage = self => ({
sendMessage: () => console.log('send message:', self.message)
});
const checkIsValidPhone = self => ({
isValid: () => console.log('valid phone', self.from)
});
const checkEmailValid = self => ({
isValidEmail: () => console.log('valid email', self.store)
});
// [2] crate object composition
const personalEnterprise = (from, message, store) => {
// [3] attributes
const self = {
from,
message,
store
};
// [4] method
const personalEnterpriseBehaviors = self => ({
createCatalog: () => console.log('Catalog has created: ', self.store)
});
// [5] create object composition
return Object.assign(self, personalEnterpriseBehaviors(self), canSendMessage(self), checkIsValidPhone(self),
checkEmailValid(self));
};
const pe1 = personalEnterprise('pengirim@gmail.com', 'hei produk baru nih', 'Dicoding Store');
pe1.createCatalog(); //Catalog has created: Dicoding Store
pe1.sendMessage(); //send message: hei produk baru nih
pe1.isValid();
pe1.isValidEmail();