Home > Mobile >  object inside a function?, function inside a function? i need to understand Node, and express
object inside a function?, function inside a function? i need to understand Node, and express

Time:03-20

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

CodePudding user response:

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.

CodePudding user response:

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);

CodePudding user response:

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.

  • Related