{Object or Scope?}
Although both objects and scopes look quite similar and are both collections of key/value pairs, they are very different and don't actually overlap.
To add a variable to a scope, you have two options:
1. Set a new variable to an initial value all in one line:
var aPerson = {name: "jess"};
- Make a statement starting with var, followed by the name you want to use for your variable, but don't assign a value yet:
var aPerson;
To add a property to an object you again have 2 options:
// To an already declared variable
aPerson.age = 25;
// To a brand new object
({name: "jess"}).age = 25;
Fallthrough
While failed variable lookups within a context fall through to the containing context, it's not the case that failed property lookups on objects fall through to a containing object:
var aPerson = {name: "jess"};
aPerson.age = 25;
log(aPerson.age.birthday); // undefined
While we are on the subject of accessing properties, there are two different ways to do it. There is dot notation (aPerson.age) or using brackets and quotation marks (aPerson["age"]).
Besides the global context object, there isn't any way to actually reference a context (even by using the this parameter), meaning that it's impossible to do things like return a context from a function, make a variable refer to one, or get an array full of contexts. However for each variable in the global context, there is a corresponding property on the global object which can be referenced in the browser as 'window':
var globalVariable = "jess";
log(globalVariable); // jess
log (window.globalVariable); // jess
var globalScope = function () {
return window;
};