I am trying to get to know OOP in JS and while trying out the basic constructor functions like this:
const SomeConstructor = function(arg) {
this.arg = arg
}
SomeConstructor.prototype.method = function()
{
console.log(`This is a function that is inherited, the argument passed in was: ${this.arg}`)
}
const someObject = new SomeConstructor(1)
someObject.method()
//Creates class, adds a method to prototype than creates object that inherites it
I wondered if it wouldn't be easier to just put the method declaration into the constructor, so the constructor would look like this:
const SomeConstructor = function(arg) {
this.arg = arg
SomeConstructor.prototype.method = function()
{
console.log(`This is a function that is inherited, the argument passed in was ${this.arg}`)
}
}
The object based on this class still kept the same structure - the method was under proto. So what's the catch? Can it be left like this or does it have some performance implications?