I mean, there's a really good chance I've just overlooked something simple here.
It's easier to explain in the code, but I'll try here too:
- I create an object with a 'medals' property set to 5.
- I console.log() the object and get the object with a medals property of 6.
- Logging the actual object property using dot notation gets 5.
- I then call a function to increment the medals property
- Logging the object displays the same object as before, with a property of 6
In node and in the live snippets feature on this site, logging the object the first time gives the correct result of 5.
But it seems that in browsers that logging the object the first time actually retrieves the object after the following lines of code have run. Is this expected behaviour, or am I being thick?!
var Person5 = function(name, YoB, height, weight) {
this.name = name;
this.YoB = YoB;
this.height = height;
this.weight = weight;
};
var Athlete5 = function(name, YoB, height, weight, olympicGames, medals) {
Person5.call(this, name, YoB, height, weight);
this.olympicGames = olympicGames;
this.medals = medals;
};
Athlete5.prototype = Object.create(Person5.prototype);
Athlete5.prototype.addMedal = function() {
console.log('Number of medals before: ' + this.medals);
console.log('Adding medal');
this.medals++;
console.log('Number of medals before: ' + this.medals);
}
var nick = new Athlete5('Nick', 1983, 6.0, 14, 5, 5);
console.log(nick); // medals: 6 (in browser, it's 5 in the snippets)????
console.log(nick.medals); // medals: 5
nick.addMedal();
console.log(nick); // medals: 6
Here's the result in Firefox, it says '5', but if you expand the object it then says '6'