-
Book Overview & Buying
-
Table Of Contents
Learning jQuery, Third Edition
By :
JavaScript is fortunate to number itself among the programming languages that support inner function declarations . Many traditional programming languages, such as C, collect all functions in a single top-level scope. Languages with inner functions, on the other hand, allow us to gather small utility functions where they are needed, avoiding namespace pollution .
An inner function is simply a function that is defined inside of another function. For example:
function outerFn() {
function innerFn() {
}
}Here, innerFn()
is an inner function, contained within the scope of outerFn()
. This means that a call to innerFn() is valid within outerFn(), but not outside of it. The following code results in a JavaScript error:
function outerFn() {
$.print('Outer function');
function innerFn() {
$.print('Inner Function');
}
}
$.print('innerFn():');
innerFn();We can successfully run the code, though, by calling innerFn() from within outerFn() as follows:
function outerFn() {...
Change the font size
Change margin width
Change background colour