Copied RSS Feed

Company

The head object vs. global properties and global variables

One of the more complex parts of JavaScript is managing the scope of variables and properties. Check out the following excerpt from JavaScript Succinctly to learn more about global properties and variables.

The head object vs. global properties and global variables

Do not confuse the head object with global properties or global variables contained within the global scope. The head object is an object that contains all objects. The term “global properties” or “global variables” is used to refer to values directly contained inside the head object and are not specifically scoped to other objects. These values are considered global because no matter where code is currently executing, in terms of scope, all code has access (via the scope chain) to these global properties and variables.

In the following sample, I place a foo property in the global scope, then access this property from a different scope.

Sample: sample.65.html

 

Had I placed the foo property outside of the global scope, the console.log function would return undefined. (The sample above is an excerpt from JavaScript Succinctly.)

Download the e-book, JavaScript Succinctly by Cody Lindley to get all the samples.

Meet the Author

Daniel Jebaraj