-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaScript Prototype Pattern example
- Loading branch information
1 parent
e83cdfc
commit b7c3505
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Monsters</title> | ||
<meta charset="utf-8"> | ||
<script> | ||
class Monster { | ||
eatsChildren = true; | ||
hasWings = false; | ||
numHeads = 1; | ||
canBreatheFire = false; | ||
constructor(name) { | ||
this.name = name; | ||
} | ||
spitPoison() { } | ||
} | ||
class Dragon extends Monster { | ||
constructor(name, hasWings) { | ||
super(name); | ||
this.hasWings = hasWings; | ||
} | ||
} | ||
class Drakon extends Monster { | ||
constructor(name, numHeads, canBreatheFire) { | ||
super(name); | ||
this.numHeads = numHeads; | ||
this.canBreatheFire = canBreatheFire; | ||
} | ||
spitPoison() { | ||
console.log(this.name + " spits poison"); | ||
} | ||
} | ||
|
||
let ladon = new Dragon("Ladon", false); | ||
let drakon = new Drakon("Drakon", 2, true); | ||
|
||
//let laconian = Object.create(drakon); | ||
let laconian = makeMonster(drakon); | ||
laconian.name = "Laconian"; | ||
|
||
function makeMonster(p) { | ||
return Object.create(p); | ||
} | ||
|
||
</script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
|