11

in the following code

const express = require('express');
const app = express()
app.use(express.static('public'));

express is a function so how it call "express.static('public')" method on it? is it possible in JavaScript to call a function which is inside a function

TheEhsanSarshar
  • 2,677
  • 22
  • 41

3 Answers3

19

A functions is not only a first class function, but also a first class object and can contain properties.

function foo() {}

foo.bar = function () { console.log('i am bar'); };

foo.bar();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 5
    Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a _function object_, with syntactic sugar that makes it look like maths function application”, i.e. `express()` would be syntactic sugar for `express.()`. From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. `express.static('public')` would be syntactic sugar for `express(, 'public')`. – leftaroundabout Dec 22 '18 at 13:40
  • 4
    @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though. – hmakholm left over Monica Dec 22 '18 at 14:42
  • @HenningMakholm The first one actually does. The "``" method is named [[call]] in the spec, although it is an internal method not a normal object property. – Bergi Dec 22 '18 at 19:38
  • @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: `foo.bar()` would then be sugar for `foo["bar"].()` plus some additional semantics to set `this` appropriately. – hmakholm left over Monica Dec 22 '18 at 21:54
3

You can attach a function as member data to another function (which is what is done in your example).

const express = () => {};
express.static = argument => console.log('argument');
express.static('public'); # console >>> 'public'

However, you cannot readily access a variable that is defined in a function body.

const express = () => {
    const static = argument => console.log('argument');
};
express.static('public'); # console >>> Error: undefined in not a function

There is a signifiant difference between member data attached to a function (the first example) and the closure that wraps the body of the function (the second example).

So, to answer your question "is it possible in JavaScript to call a function which is inside a function?" No, this is not readily possible with the important note that this is not what is being done in your example.

Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
-1

Actually, it is possible to call a function inside another function in javaScript. So it depends on the condition, Callback is widely used in js

(A callback is a function that is to be executed after another function has finished executing)

function doHomework(subject, callback) {
  alert(`Starting my ${subject} homework.`);
  callback();
}
function alertFinished(){
  alert('Finished my homework');
}
doHomework('math', alertFinished);

And the second example can be Closures

(A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables)

function outer() {
   var b = 10;
   function inner() {

         var a = 20; 
         console.log(a+b);
    }
   return inner;
}
  • 3
    While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question. – Pac0 Dec 22 '18 at 14:06
  • @Pac0 I believe this answer fully satisfies **(is it possible in JavaScript to call a function which is inside a function)** this part of the question. So there is no reason to vote as a not useful answer! – Nuriddin Kudratov Dec 22 '18 at 21:27
  • @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (`express` can be executed `()` and also has `static` as member). This case is not among your examples. – Pac0 Dec 23 '18 at 09:45