-1

I'm trying to set up a Node.js server using Express framework. I see a function but it is being used as an Object. I don't understand what this mean. Please explain for me.

const Users = mongoose.model('Users', userSchema);
console.log(typeof Users); // "function"

But I can use it as an Object as below:

Users.find({}); // What this mean?????
Ajay Kumar Ganesh
  • 1,838
  • 2
  • 25
  • 33
Xuân Việt
  • 45
  • 1
  • 8

4 Answers4

1

Every function in JavaScript is a Function object. Functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

Understand method chaining

Function Reference: Mozilla Dev

Ajay Kumar Ganesh
  • 1,838
  • 2
  • 25
  • 33
0

Functions are, at the end, just objects in JavaScript:

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions)

fjc
  • 5,590
  • 17
  • 36
0

Functions are also objects in js, so you can add attributes to them.

The function mongoose.model() returns a function which can be instantiated new User(...) or used as a Class which provides a set of static functions.

Ele
  • 33,468
  • 7
  • 37
  • 75
  • Yeah, as you say. It's right here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static – Xuân Việt Apr 25 '19 at 17:47
0

We see that a function is indeed an object. JavaScript functions are a special type of objects, called function objects. A function object includes a string which holds the actual code -- the function body -- of the function

For more details visit here

Murtaza Hussain
  • 3,851
  • 24
  • 30