Cover by Cody Lindley

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 5. The Head/Global Object

Conceptual Overview of the Head Object

JavaScript code, itself, must be contained within an object. As an example, when crafting JavaScript code for a web browser environment, JavaScript is contained and executed within the window object. This window object is considered to be the “head object,” or sometimes confusingly referred to as “the global object.” All implementations of JavaScript require the use of a single head object.

The head object is set up by JavaScript behind the scenes to encapsulate user-defined code and to house the native code with which JavaScript comes prepackaged. User-defined code is placed by JavaScript inside the head object for execution. Let’s verify this as it pertains to a web browser.

Below, I am creating some JavaScript values and verifying the values are placed in the head window object.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myStringVar = 'myString';
var myFunctionVar = function() {};
myString = 'myString';
myFunction = function() {};

console.log('myStringVar' in window); // returns true
console.log('myFunctionVar' in window); // return true
console.log('myString' in window); // returns true
console.log('myFunction' in window); // return true

</script></body></html>

You should always be aware that when you write JavaScript, it will be written in the context of the head object. The remaining material in this chapter assumes you are aware that the term “head object” is synonymous with “global object.” ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required