Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between mongoose.Schema() and new mongoose.Schema()?

What is the difference between mongoose.Schema() and new mongoose.Schema()?

I used both the standards, and I think both do the same job. Does it affect in any way?

like image 677
Madhubala Jayakumaran Avatar asked Oct 25 '25 04:10

Madhubala Jayakumaran


1 Answers

Looking at the docs, it should always be called with the new keyword, since Schema is a constructor.

Looking at the source code, it'll return a newly allocated object if you try to use it without the new keyword.

Both will work, but I would only use the first since it's the correct way. Following standard conventions will make your code easier to read; not only for others, but for you as well when you go back to it in 6 months.

like image 102
mastermind202 Avatar answered Oct 26 '25 18:10

mastermind202



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!