Chapter 3. Closure Library Primitives

As demonstrated in Chapter 1, the one JavaScript file used to bootstrap the rest of the Closure Library is base.js. This is where the root object, goog, is created, to which all other properties in the Closure Library are added. Because all of the functions defined in base.js are available to any JavaScript code that uses the Closure Library, they are, in a sense, primitives of the Library. This chapter is a comprehensive reference for these primitives.

In enumerating the API of base.js, this chapter also aims to explain how some high-level concepts are designed to work in the Closure Library, and to provide insight into the Library’s design. Each section is intended to introduce an idiom of the Library, and the subsections list the variables and functions in base.js that support that idiom.

Dependency Management

The “Hello World” example in Chapter 1 demonstrated that goog.provide() and goog.require() are used to establish dependency relationships in the Closure Library. This section discusses the mechanics of how these functions implement the dependency management system for the Library.

calcdeps.py

Unlike Java, in which multiple interdependent classes can be compiled together, JavaScript files cannot contain forward declarations due to the fact that JavaScript files are evaluated linearly as determined by <script> tag order in a web page. To produce an ordering without forward declarations, calcdeps.py uses goog.require() and goog.provide() calls ...

Get Closure: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.