0

I just started learning JavaScript and Node. Since coming from Python background few things seems awkward. For example to create a server in Express we will use:

const express = require("express");

const app = express();
...

where it says

/**
 * Creates an Express application. The express() function is a top-level function exported by the express module.
 */

I don't understand why express() is a function. If the same thing should be done in Python, it will be done like:

from flask import Flask
app = Flask(__name__)

where it says

First we imported the Flask class. An instance of this class will be our WSGI application.

Next we create an instance of this class. The fi...

And this is my general understanding about programming. Most of the time we import some class from a library and make one object and after that, we manipulate its methods. In javascript I find it confusing with function, constructor function, etc. Is a constructor function the same as a class?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Epsi95
  • 8,832
  • 1
  • 16
  • 34
  • 1
    Maybe run through e.g. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS. Note that there's really nothing substantially different here; in both cases you bring a name into the current scope and call it to get an application. – jonrsharpe May 17 '20 at 17:16
  • Ok, it seems there is no notion of class although ES6 introduced classes. It's little bit quirky though. – Epsi95 May 17 '20 at 21:19

1 Answers1

2

When you import express, what you get is a factory function. A factory function is a function that, when you call it, it creates an object for you. It's similar to using new with a constructor, but a factory function doesn't expose the constructor directly or the class directly. It just exposes you to a function that when you call it, it creates an object that you can then interact with by calling methods.

Imagine this behind the scenes:

class Express {
    constructor() {
        ...
    }
    get() {
        ...
    }
    post() {
        ...
    }
    use() {
        ...
    }
}

// factory function
function createExpressInstance() {
   let obj = new Express();
   // maybe do some other setup on the object
   return obj;
}

// define function that will be referenced as express.static()
createExpressInstance.static = function() {
   ...
}

// export the factory function
module.exports = createExpressInstance;

A factory function is one design pattern for an API that can create new objects. Rather than exposing the class definition directly or the exporting the constructor directly, it "hides" the actual implementation. You call a function and get an object back.

There are some specific situations where a factory function is a preferred way of constructing a new object (particularly when asynchronous things might be involved in constructing the object), but in other situations (like this), it's just a style choice for how to write the code and how to design a public API.

In Javascript, is a constructor function the same as a class?

Yes and no. The class is the whole definition, including the methods. The constructor is just one method of that class definition.

But, you call the constructor by using the name of the class. So if (in my example above), you did console.log(typeof Express), you would get "function" because the symbol Express represents the constructor function.

jfriend00
  • 683,504
  • 96
  • 985
  • 979