Last active
April 14, 2020 12:54
-
-
Save oelna/3d86ca57452e4ddc8b6f854ef6a56b67 to your computer and use it in GitHub Desktop.
A simple hacked-together demo of Javascript modules and classes
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
const CHARACTER = class { | |
constructor(name, attributes = {}) { | |
this.name = name; | |
this.attributes = {}; | |
// merge custom with default values | |
Object.assign(this.attributes, { | |
'strength': 3, | |
'constitution': 3, | |
'dexterity': 3, | |
'intelligence': 3, | |
'wisdom': 3, | |
'charisma': 3 | |
}, attributes); | |
} | |
attack() { | |
alert('boink!'); | |
} | |
} | |
export {CHARACTER}; |
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> | |
<head> | |
<title>module and class demo</title> | |
</head> | |
<body> | |
<h1>It works!</h1> | |
<script type="module"> | |
// https://javascript.info/import-export | |
import {CHARACTER} from './character.js'; | |
let warrior = new CHARACTER('Hector', { | |
'strength': 10, | |
'charisma': 5, | |
'dexterity': 7 | |
}); | |
console.log(warrior.name, warrior.attributes); | |
warrior.attack(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment