Created
June 12, 2023 16:29
-
-
Save cferdinandi/9ca0535a6274696db633abe76a8d14c7 to your computer and use it in GitHub Desktop.
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
<!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> |
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
<!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