mardi 20 octobre 2015

Cannot reference script-level variable in function?

My code:

f();

var a = {v: 10};

function f() {
    console.log(a.v);
}

Gives the error that undefined has no property v.

However the following code works:

var a = {v: 10};

var f = function() {
    console.log(a.v);
}

f();

Why doesn't the first version work?




Aucun commentaire:

Enregistrer un commentaire