JavaScript Nested Function

I have a piece of code for javascript that I just don't understand:

function dmy(d) { function pad2(n) { return (n < 10) ? '0' + n : n; } return pad2(d.getUTCDate()) + '/' + pad2(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear(); } function outerFunc(base) { var punc = "!"; //inner function function returnString(ext) { return base + ext + punc; } return returnString; } 

How can a function be defined as part of another function? Can we call pad2 () outside of my ()?

Enlighten him. Thanks

+63
javascript nested-function
Sep 03 '11 at 20:16
source share
6 answers

Functions are another type of variable in JavaScript (with some nuances, of course). Creating a function inside another function changes the scope in the same way as changing the scope of a variable. This is especially important for use with closure to reduce overall global namespace pollution.

Functions defined in another function will not be accessible outside the function unless they are bound to an object accessible outside the function:

 function foo(doBar) { function bar() { console.log( 'bar' ); } function baz() { console.log( 'baz' ); } window.baz = baz; if ( doBar ) bar(); } 

In this example, the baz function will be available for use after executing the foo function, since it is overridden by window.baz . The bar function will not be available in any context other than the areas contained in the foo function.

as another example:

 function Fizz(qux) { this.buzz = function(){ console.log( qux ); }; } 

The Fizz function is intended as a constructor, so when it starts, it assigns the buzz function to the newly created object.

+104
Sep 03 2018-11-11T00:
source share

It is called closure .

Basically, a function defined inside another function is only available inside that function. But it can be transmitted as a result, and then this result can be called.

This is a very powerful feature. Here you can see more explanations:

javascript_closures_for_dummies.html mirror on Archive.org

+25
Sep 03 '11 at 20:30
source share
 function x() {} 

equivalent (or very similar) to

 var x = function() {} 

if I'm not mistaken.

So, nothing funny happens.

+11
Sep 03 '11 at 20:24
source share

An instance function is allowed inside and outside functions. Inside these functions, like variables, nested functions are local and therefore cannot be obtained from the external area.

 function foo() { function bar() { return 1; } return bar(); } 

foo manipulates the bar inside itself. bar can not touch the outer region if it is not defined in the outer region.

So this will not work:

 function foo() { function bar() { return 1; } } bar(); // throws error: bar is not defined 
+6
Sep 03 '11 at 20:26
source share

When you declare a function inside a function, internal functions are only available in the area in which they are declared, or in your case, pad2 can only be called in the dmy .

All variables existing in dmy are visible in pad2 , but this does not happen the other way around: D

+4
Sep 03 2018-11-11T00:
source share

It is perfectly normal in Javascript (and many languages) to have functions inside functions.

Take the time to learn the language, do not use it on the grounds that it is similar to what you already know. I suggest watching the Douglas Crockford series from YUI Javascript presentations with special emphasis on Action III: Ultimate feature (link to download videos, slides and transcripts)

+3
Sep 03 2018-11-11T00:
source share



All Articles