06.07.2017 Views

Mastering JavaScript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Functions, Closures,<br />

and Modules<br />

In the previous chapter, we deliberately did not discuss certain aspects of <strong>JavaScript</strong>.<br />

These are some of the features of the language that give <strong>JavaScript</strong> its power and<br />

elegance. If you are an intermediate- or advanced-level <strong>JavaScript</strong> programmer, you<br />

may be actively using objects and functions. In many cases, however, developers<br />

stumble at these fundamental levels and develop a half-baked or sometimes<br />

wrong understanding of the core <strong>JavaScript</strong> constructs. There is generally a<br />

very poor understanding of the concept of closures in <strong>JavaScript</strong>, due to which<br />

many programmers cannot use the functional aspects of <strong>JavaScript</strong> very well. In<br />

<strong>JavaScript</strong>, there is a strong interconnection between objects, functions, and closures.<br />

Understanding the strong relationship between these three concepts can vastly<br />

improve our <strong>JavaScript</strong> programming ability, giving us a strong foundation for any<br />

type of application development.<br />

Functions are fundamental to <strong>JavaScript</strong>. Understanding functions in <strong>JavaScript</strong><br />

is the single most important weapon in your arsenal. The most important fact<br />

about functions is that in <strong>JavaScript</strong>, functions are first-class objects. They are<br />

treated like any other <strong>JavaScript</strong> object. Just like other <strong>JavaScript</strong> data types, they<br />

can be referenced by variables, declared with literals, and even passed as function<br />

parameters.<br />

[ 45 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!