Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created June 12, 2023 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/9ca0535a6274696db633abe76a8d14c7 to your computer and use it in GitHub Desktop.
Save cferdinandi/9ca0535a6274696db633abe76a8d14c7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Classes</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
class Greeting {
#name;
static #emoji = '🎉';
constructor (name) {
this.#name = name;
}
sayHi () {
console.log(`Hello ${this.#name}!`);
}
sayBye () {
console.log(`See you later, ${this.#name}.`);
}
static party () {
console.log(`Party time! ${Greeting.#emoji}`);
}
}
let merlin = new Greeting('Merlin');
let ursula = new Greeting('Ursula');
merlin.sayHi();
ursula.sayBye();
Greeting.party();
console.log(Greeting.#emoji);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Constructor Pattern</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
let Greeting = (function () {
let emoji = '🎉';
function Greeting (name) {
this.name = name;
}
Greeting.prototype.sayHi = function () {
console.log(`Hello ${this.name}!`);
};
Greeting.prototype.sayBye = function () {
console.log(`See you later, ${this.name}.`);
};
Greeting.party = function () {
// console.log(`Let's party, ${this.name}!`);
console.log(`Party time! ${emoji}`);
};
return Greeting;
})();
let merlin = new Greeting('Merlin');
let ursula = new Greeting('Ursula');
merlin.sayHi();
merlin.name = 'Poopyhead';
merlin.sayBye();
// ursula.sayBye();
// merlin.party();
// Greeting.party();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment