I thought I had a good understanding of async / sync / hoisting, when it came to how a browser loads data and presents it to the user. However this following example is throwing me for a loop, let me post the example and then explain the question:
var obj = {
counter: 0,
};
Object.defineProperty(obj, 'reset', {
get: function() {
return this.counter = 2;
}
});
Object.defineProperty(obj, "increment", {
get: function() {
return this.counter++;
}
});
Object.defineProperty(obj, "decrement", {
get: function() {
return this.counter--;
}
});
console.log(obj.reset) //2
console.log(obj.increment + ' ' + "incremented") // "2 incremented"
console.log(obj.decrement + ' ' + "decremented") // "3 decremented"
I thought that the way the browser would interpret this would be to run every line of code synchronously which should produce:
//2
//2 incremented
//2 decremented
because if you where to track the changes to 'counter' all the way until the console logs are called, counter would change from 0 to 2 to 3 back down to 2.
And then I thought well wait a second; if the values being returned are not all 2 then maybe calling these console logs is an asynchronous way about getting the data which would then logically lead me to think the result should be:
//2
//3
//2
because if you where to access the 'counter' property upon the time of running the console log then logicly the counter value would change from 0 to 2 upon (obj.reset) and then 3 upon(obj.increment) and then 2 again upon(obj.decrement)
I am obviously wrong about the way im approaching how the browser interprets this and would like a clear step by step explanation of why the values being returned are:
//2
//2 incremented
//3 decremented
if at all possible, thank you.