I was going to start using the Discord.js library to create a bot, but came across this first line of code:
const Discord = require('discord.js');
const client = new Discord.Client();
I was then curious to see how we could call the class without parentheses and call what almost looks like a static method... So i went about trying to recreate an example of how I might try to use something like this.
What I tried:
//this worked! but why?
class Discord{
constructor(){
}
}
Discord.Client = function(){
console.log('this is client in a static method?')
}
new Discord.Client() //i could call the class using the key word 'new' and call the client static method. console.log --> this is client in a static method?
but then when i tried something like this and could not call class with new:
class Discord{
constructor(){
}
static Client(){
console.log('client static method')
}
}
new Discord.Client()//Uncaught TypeError: Discord.Client is not a constructor
Discord.Client()//console.log --> client static method
I'm trying to understand what the difference is between the two, and please correct me if I'm wrong on any of this.
Thank you for taking the time to read this. Your help is greatly appreciated!!