0

Um can someone help me with the contact manager question can't seem to find the bug

function contact(name, number) { this.name = name; this.number = number; { console.log( this.name+":",this.number); } } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print(); b.print();

11th Aug 2022, 9:49 PM
CerealAgenda
CerealAgenda - avatar
3 Answers
+ 4
The example provided below uses the ES5 function declaration that may be more familiar ECMAScript 5 Example: function contact(name, number) { this.name = name; this.number = number; this.print = print; } function print() { console.log(this.name + ": " + this.number); } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print(); SoloProg example uses the Javascript ES6 arrow function. That comes much later in lesson 57 ECMAScript 6 Example: function contact(name, number) { this.name = name; this.number = number; this.print = () => console.log( this.name + ":" , this.number); } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print();
12th Aug 2022, 1:18 AM
Chris Coder
Chris Coder - avatar
+ 2
// Try it this.print = () => console.log( this.name + ":" , this.number);
11th Aug 2022, 10:26 PM
SoloProg
SoloProg - avatar
+ 1
You missed to add method inside object constructor function, you just type { console.log(...))} Replace that part of code with code SoloProg posted and than you can access print method of objects you have created. Now your object dont have print method and you get error.
12th Aug 2022, 12:16 AM
PanicS
PanicS - avatar