I'm working on constructor, at first I was using ES5 and my error was "Message is not a function" then I saw something about arrow function not been used with constructor so I removed it and got another error saying "Maximum call stack size exceeded" something about recursion. I don't know what exactly I'm doing wrong. have seen a lot of answers on constructor but none of them seems helpful.
function Message(name, motive, hubby){
this.name = name
this.motive = motive
this.hubby = hubby
this.output=function(){
return `Hi, i'm ${this.name} i love ${this.hubby} and i would love to be your ${this.motive}.`
}
var Mymessage = new Message('josephine','friend', 'coding');
console.log(Mymessage.output());
}
Message();
This is how you should create an instance and call a function of that instance
function Message(name, motive, hubby){
this.name = name
this.motive = motive
this.hubby = hubby
this.output=function(){
return `Hi, i'm ${this.name} i love ${this.hubby} and i would love to be your ${this.motive}.`
}
}
var Mymessage = new Message('josephine','friend', 'coding');
console.log(Mymessage.output());
function Message(name, motive, hubby){
this.name = name
this.motive = motive
this.hubby = hubby
this.output=function(){
return `Hi, i'm ${this.name} i love ${this.hubby} and i would love to be your ${this.motive}.`
}
}
var Mymessage = new Message('josephine','friend', 'coding');
console.log(Mymessage.output());
Message();
You must write var Mymessage outside the Message function , as you have called it inside the same function, that line of code is calling itself again and again which is causing an erro .