Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a function on a function is it possible?

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

like image 316
TheEhsanSarshar Avatar asked Dec 22 '18 09:12

TheEhsanSarshar


People also ask

Can I call a function in a function?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can a function be called by another function?

It is important to understand that each of the functions we write can be used and called from other functions we write. This is one of the most important ways that computer scientists take a large problem and break it down into a group of smaller problems.

What is it called when a function calls a function?

A function call is an expression containing the function name followed by the function call operator, () . If the function has been defined to receive parameters, the values that are to be sent into the function are listed inside the parentheses of the function call operator.

Can you call a function in a function in C?

No you can't have a nested function in C . The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.


2 Answers

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();
like image 53
Nina Scholz Avatar answered Oct 26 '22 21:10

Nina Scholz


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.

like image 22
Jared Goguen Avatar answered Oct 26 '22 21:10

Jared Goguen