I am trying to follow Kyle Simpson's idea of actual prototypal inheritance using only objects instead of functions. I wanted to see if I could use Object.create and Object.assign to add mixins to an object, which totally works. I then tried to take it a step further and base some of the values of the properties in a mixin on the value of a property in the base object. I always get NaN for the value I'm trying to work with. Here's the code:
function generatorFactory() {
var basePlayer = {
x: 0,
y: 0,
baseHealth: 50
};
var generatePlayer = function() {
return basePlayer;
};
return generatePlayer;
}
var playerGenerator = generatorFactory();
var basicPlayerObject = playerGenerator();
var heavyArmor = {
defense: 15,
attack: 9
};
var lightArmor = {
defense: 7
};
var swordsman = {
health: this.baseHealth + 10
};
var knifeFighter = {
health: this.baseHealth - 10
};
var sigurd = Object.assign(Object.create(basicPlayerObject), heavyArmor, swordsman);
console.log(sigurd.health);
console.log(sigurd.defense);
console.log(sigurd.attack);
console.log("Player coordinates: (" + sigurd.x + ", " + sigurd.y +")");
I want to have the swordsman mixin adjust the player's base health. I tried using 'this', but it did not help. I'm starting to think I'm approaching this completely wrong. What exactly am I doing wrong here?