Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. The Head/Global Object > The Head Object versus Global Properties and Global...

The Head Object versus 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/variables.

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

Live Code

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

var foo = 'bar'; /* foo is a global object and a property of the 
                 head/window object */

var myApp = function() { // remember functions create scope
   var run = function() {
       // logs bar, foo's value is found via the scope chain in the head object
       console.log(foo);
   }();
}

myApp();

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

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint