Skip to content

Instantly share code, notes, and snippets.

@oelna

oelna/character.js

Last active Apr 14, 2020
Embed
What would you like to do?
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