The reason i am asking is because this code which has an array directly declared into the object will throw the following error: Uncaught ReferenceError: projects is not defined
var client = {
projects: ['a', 'b', 'c', 'd'],
test: function() {
return projects.length;
}
};
console.log(client.test());
Of course we can just declared the variable and then reference it within the object like this:
var projects = ['a', 'b', 'c', 'd'];
var client = {
projects: projects,
test: function() {
return projects.length;
}
};
console.log(client.test());
And that would work. But i really want to know why in the world is the first example giving the error. What is wrong with it??