Created
December 15, 2019 18:36
-
-
Save Theo-denBlanken/978a7f1bba7aacf0af2fc82b21d8aa7a to your computer and use it in GitHub Desktop.
Een voorbeeld van een JavaScript class, waarbij 2 instanties uit de Class auto worden gemaakt en gebruikt
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="nl"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Test auto Class</title> | |
<style> | |
.auto { | |
width: 2em; | |
height: 1.5em; | |
position: absolute; | |
} | |
body {overflow: auto;} | |
</style> | |
</head> | |
<body lang="nl"> | |
<h1>Test auto Class</h1> | |
<script> | |
class Auto { | |
constructor( horizontaal, verticaal, snelheid, kleur ) { | |
this._hori = horizontaal; | |
this._vert = verticaal; | |
this._snelh = snelheid; | |
this._kleur = kleur; | |
} | |
// method maken: creëer een element en plaats deze in de body | |
maken() { | |
let auto = document.createElement('div'); | |
auto.className = 'auto'; | |
auto.style.top = this._vert + 'px'; | |
auto.style.left = this._hori + 'px'; | |
auto.style.background = this._kleur; | |
document.body.appendChild(auto); | |
} | |
} | |
// twee instanties van de class Auto maken | |
let auto1 = new Auto(Math.random()*200, Math.random()*500, 4, 'red'); | |
auto1.maken(); | |
let auto2 = new Auto(Math.random()*200, Math.random()*500, 4, 'green'); | |
auto2.maken(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment