Variables are scanned in the scope chain, starting with the current execution context and raising the tree that spans the execution contexts.
Properties are checked first on the base object, and then on the object [[Prototoype]] chain (ie, on the internal prototype).
So, if you do:
foo
foo will be considered as a variable and will look for a chain of scopes. Variable names are never qualified; you cannot direct them to the specific execution context to look for. If there are two variables in the chain of regions with the same name, you can only access the one that first occurs when the chain moves (there is a way around this especially for global variables), for example
var a = 'global a'; function foo() { var a = 'local a'; return a; } console.log(foo());
In the above, a inside the function resolves the local variable a. In the case of global variables, they become properties of the global object, so you can access them even if they are "obscured" by the same local property name, for example.
function foo() { var = 'local a';
In contrast, property names are always preceded by the underlying object on which they are viewed (as in the example above, where it refers to a global object), for example
foo.bar
will be split into foo and bar. First, foo will be resolved in the scope chain, and if found, resolving the property will try to find the bar property. Therefore, for properties, you can direct the object that the object was looking for. Therefore, if there are two objects with the same named property, you can search for both properties while both objects are in scope.
So, the first part of any link is considered as a variable, the subsequent parts are considered as properties. Except when used, but not recommended. Do not go there.
But for completeness ... with the places of the specified object in the start-up chain of objects, so first the variables are first considered as properties of this object before using the scope chain, so you can do:
var cos = function(arg){return 'my cos function: ' + arg}; function foo() { // cos is resolved on the scope chain console.log(cos(0.5)); // my cos function: 0.5 with (Math) { // cos is first resolved as a property of Math, and only on the // scope chain if not found there console.log(cos(0.5)) // 0.8775825618903728 } } foo();