Skip to content

Instantly share code, notes, and snippets.

@oelna
Last active April 14, 2020 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelna/3d86ca57452e4ddc8b6f854ef6a56b67 to your computer and use it in GitHub Desktop.
Save oelna/3d86ca57452e4ddc8b6f854ef6a56b67 to your computer and use it in GitHub Desktop.
A simple hacked-together demo of Javascript modules and classes
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};
<!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