JavaScript contact manager help - “A.print is not a function” error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript contact manager help - “A.print is not a function” error

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

17th Jan 2021, 12:16 AM
Patricia Lim
3 Answers
+ 2
function contact(name, number) { this.name = name; this.number = number; this.print = print; // remove () } function print() { console.log(this.name + ": " + this.number); // "this" refers to the calling object } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print(); Don't use () when assigning a function as a class' member, when you use () with a function name, it means you are calling the function rather than assigning the function as a class' member. Use `this` to refer the calling object when you need to access the said object's member.
17th Jan 2021, 12:32 AM
Ipang
+ 1
this operator is designed for class, we don't need to use this in functional module. function contact(name, number) { function print (){ console.log(name + ":" + number); } return {print}; } var a = contact("David", 12345); var b = contact("Amy", 987654321) a.print(); b.print(); https://code.sololearn.com/Wgij9LdFG3ZL/?ref=app
17th Jan 2021, 1:28 AM
Calviղ
Calviղ - avatar
0
Calviղ this assignment is in objects section. I think. Constructor functions are very very important part object oriented javascript. You don't need to use "this" in this assignment but it is very important to understand "this" in general.
17th Jan 2021, 10:40 AM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar