Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
object inside a function?, function inside a function? i need to understand Node, and express

0

I have a question, what contains the function express()? , because i cannot understand how you call a function if you store express() in a variable like: const app = express();

and then you call , for example, the function listen() like if was an object:

app.listen()

can you guys help me to understand?

thanks in advance

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Functions in Javascript are objects. You can assign them to variables. You can execute them by calling them. You can assign properties to them. You can read properties from them.

So, this is assigning a function to a variable (assigns the module.exports from the 'express' module to a variable named express):

const express = require('express');

This is executing the function by calling it:

const app = express();

This is accessing the .static property on the express function (which can also be a function):

app.use(express.static('/public'));

As another example, all regular functions have some built in properties:

function greet(greeting) {
    console.log(greeting);
}

// outputs 1 because the function has one declared argument
console.log(greet.length);    

But, you can also add your own properties if you want since functions are a sub-class of an object.

about 4 years ago · Juan Pablo Isaza Report

0

Objects can have methods. Methods are really just functions. Sometimes those methods return something, and other times they don't. Sometimes you care about the result, and sometimes you don't.

const myObj = {
  add: (a, b) => a+b;
  echo: (msg) => console.log(msg);
}

const result = myObj.add(1,2);
myObj.echo(result);
about 4 years ago · Juan Pablo Isaza Report

0

A function nested inside another cannot be called directly from outside of the outside function.

However, an inner function can be made accessible if a global reference is made to it.

In my snipped example, outerFunction contains two innerFunctions followed by two variable declarations that assign those functions to each of two variables (named outerFunction.innerFunction1 and outerFunction.innerFunction2). Had these been declared with let or const inside the outer function, they would be accessible only from within the function. However, declaring a variable by name only, gives it global scope.

So, if outerFunction is invoked, it places the inner functions into the global scope. They can now be referenced by the names they were given outerFunction.innerFunction1 and outerFunction.innerFunction2. Giving them names containing a dot is useful as a reminder of where the functions come from, but they could be given any name and called directly (as in my third example in the snippet).

function outerFunction(){
    
    function innerFunction1(){
      console.log( 'hello from first innerFunction' );
    } // end innerFunction2;
    
    function innerFunction2(){
      console.log('hello from second innerFunction');
    } // end innerFunction2;

    function innerFunction3(){
      console.log('hello from the third function');
    } // end innerFunction3;
    
  outerFunction.innerFunction1 = innerFunction1;
  outerFunction.innerFunction2 = innerFunction2;
  anyName = innerFunction3;
  
} // end outer function;

outerFunction(); // essential to declare and initialise global variables about to be accessed;
outerFunction.innerFunction1();
outerFunction.innerFunction2();
anyName();

If you can look inside outer function of the example you gave, you will find a declaration to the dot notation variable name you're using to call it. There is nothing magical about the dot notation here, it is just a variable name.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!