getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.87k stars 33.44k forks source link

async & performance:ch1:Statement Ordering:example code error? #1642

Closed londbell closed 4 years ago

londbell commented 4 years ago
function foo() {
    console.log( b );
    return 1;
}

var a, b, c;

// ES5.1 getter literal syntax
c = {
    get bar() {
        console.log( a );
        return 1;
    }
};

a = 10;
b = 30;

a += foo();             // 30
b += c.bar;             // 11

console.log( a + b );   // 42

I think it shoud be:

function foo() {
    console.log( b );
    return 1;
}

var a, b, c;

// ES5.1 getter literal syntax
c = {
    get bar() {
        console.log( a );
        return 1;
    }
};

a = 10;
b = 30;

a += foo();             // 11
b += c.bar;             // 30

console.log( a + b );   // 42
getify commented 4 years ago

The code is correct, as those commented outputs are from the console.log() statements, not the operations themselves.