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

167
Views
JS variable that is both object and function on call

Let me start with some statements

-> (Object instanceof Function) because object constructor is a function

-> (Function instanceof Object) because functions are objects

Now my question is : How can I simulate the express.js object behaviour ?

const express = require("express");
const app = express(); // express here is a function
app.use(express.json()) // now here express is an object

This works fine in nodejs and i believed that express is basically an object and when you call express() you reference the object constructor. However things seem to be a bit different.

Trying to consider it as a class will also fail because you don't instantiate it as new. And, if you try to simulate it as a function, you can't simulate it as an object.

I've appended 3 of my attempts to simulate this behavior

// Trying to simulate it with a function

var express = () => { 
  this.json = () => {console.log("json")}
  return {
    listen : () =>{ console.log("listen !") },
    use    : () =>{ console.log("use !")  }
  }
}

const app = express(); // ok
      app.listen();    // ok
      app.use();       // ok

express.json(); // fail

// Trying to simulate it with a class
class express{
  constructor(){
    return {
      listen : () => { console.log("listen !") },
      use    : () => { console.log("use !")  }
    }
  }
  json(){console.log("json")}
}

const app = express(); // Class constructor express cannot be invoked without 'new'",
app.listen();  
app.use();
      
express.json(); // not instanciated

// Trying to simulate it as an object
var express = {
    constructor : ()=>{ // does not overwrite constructor 
      return {
          listen : () =>{ console.log("listen !") },
          use    : () =>{ console.log("use !")  }
      }
    },
    json : ()=>{console.log("json")}
  }
  
const app = express(); // "message": "Uncaught TypeError: express is not a function",
      app.listen();    
      app.use();       
express.json()

almost 4 years ago · Santiago Trujillo
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!