Created
February 25, 2023 02:00
-
-
Save LucasBadico/cb1e5bb773e04ab888e744bc15deec15 to your computer and use it in GitHub Desktop.
objeto vs tipo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Notifier { | |
private subscribers: ((notification: Notification) => void)[] = []; | |
constructor(subscribers: ((notification: Notification) => void)[]) { | |
this.subscribers = subscribers; | |
} | |
public notify(notification: Notification) { | |
this.subscribers.forEach((subscriber) => subscriber(notification)); | |
} | |
} | |
// Criando uma lista de subscribers | |
const subscribers = [ | |
(notification: Notification) => { | |
console.log(`Notificação recebida: ${notification.title}`); | |
}, | |
(notification: Notification) => { | |
// Aqui dentro da função, você pode fazer outras coisas com a notificação | |
alert(`Notificação recebida: ${notification.title}`); | |
} | |
]; | |
// Criando uma nova instância da classe Notifier com a lista de subscribers | |
const notifier = new Notifier(subscribers); | |
// Criando uma nova notificação | |
const notification: Notification = { | |
title: "Nova mensagem", | |
message: "Você recebeu uma nova mensagem de Fulano de Tal", | |
date: new Date(), | |
isRead: false | |
}; | |
// Notificando os subscribers com a nova notificação | |
notifier.notify(notification); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Notification = { | |
title: string; | |
message: string; | |
date: Date; | |
isRead: boolean; | |
} | |
function sendNotification(notification: Notification) { | |
// Aqui dentro da função, você pode acessar as propriedades da notificação | |
// como `notification.title`, `notification.message`, `notification.date` e `notification.isRead` | |
console.log(`Nova notificação recebida: ${notification.title} - ${notification.message}`); | |
} | |
// Criando uma nova notificação | |
const notification: Notification = { | |
title: "Nova mensagem", | |
message: "Você recebeu uma nova mensagem de Fulano de Tal", | |
date: new Date(), | |
isRead: false | |
}; | |
// Chamando a função sendNotification e passando a notificação como argumento | |
sendNotification(notification); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment