Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Nested function

I got a piece of code for javascript which I just do not 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 within another function? Can we call pad2() from outside of my() function?

Please put some light on it. Thanks

like image 261
Thomas Avatar asked Sep 03 '11 20:09

Thomas


People also ask

Are nested functions bad practice JavaScript?

no, there's nothing wrong with that at all, and in js, it's usually a good thing. the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling.

What is an example of a nested function?

Users typically create nested functions as part of a conditional formula. For example, IF(AVERAGE(B2:B10)>100,SUM(C2:G10),0). The AVERAGE and SUM functions are nested within the IF function.

What is difference between nested function and closure in JavaScript?

A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked. JavaScript 1.2 allows function definitions to be nested within other functions as well. Still, there is a restriction that function definitions may not appear within loops or conditionals.

What is nesting in JavaScript?

Nesting is when you write something inside of something else. You can have a function inside of another function: function x () { function y() { // something; } } You can have an if condition inside of another if condition: if (daylight) { if (before 12) { //It's morning; } else { // it's afternoon; } }


1 Answers

Functions are another type of variable in JavaScript (with some nuances of course). Creating a function within another function changes the scope of the function in the same way it would change the scope of a variable. This is especially important for use with closures to reduce total global namespace pollution.

The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is 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 the foo function has been run, as it's overridden window.baz. The bar function will not be available to any context other than scopes contained within the foo function.

as a different example:

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

The Fizz function is designed as a constructor so that, when run, it assigns a buzz function to the newly created object. That is, you'd use it like this:

const obj = new Fizz(); obj.buzz(); 

or more concisely (if you don't need to keep the object after calling buzz):

new Fizz().buzz(); 
like image 119
zzzzBov Avatar answered Oct 16 '22 05:10

zzzzBov