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 > Referring to the Head Object

Referring to the Head Object

There are typically two ways to reference the head object. The first way is to simply reference the name given to the head object (e.g., in a web browser this would be window). The second way is to use the this keyword in the global scope. Each of these are detailed in the code below.

Live Code

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

var foo = 'bar';

windowRef1 = window;
windowRef2 = this;

console.log(windowRef1, windowRef2); // logs reference to window object

console.log(windowRef1.foo, windowRef2.foo); // logs 'bar', 'bar'

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

In the code above, we explicitly store a reference to the head object in two variables that are then used to gain access to the global foo variable.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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