+ 1
I'm trying to solve the code project in js called contact manager but I don't really know what I'm doing
The examples in that module were very poor and difficult to understand for such a key part of js any ways this is my code function contact(name, number) { this.name = name; this.number = number; } function print(){ console.log(contact.name +": "+contact.number) } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print(); b.print(); It's is supposed to output in this format : name: number.
2 Respuestas
+ 1
Here is the correct code :
function contact(name, number) {
this.name = name;
this.number = number;
this.print = function() {
console.log(this.name +": "+this.number);
}
}
var a = new contact("David", 12345);
var b = new contact("Amy", 987654321);
a.print();
b.print();
Explanation :
1_You can't use contact.name as name isn't a static property, so you have to use instead this.name , its nearly the same concept with variables
2_In the contact object, there isn't a print function, as this function is externe, so you have code this function inside the contact object to use it (with the anonymous function syntax)
NOTE : I am making a serie of tutorials of things related to coding, so you can check this page and comment what you want to learn in particular (and following me will notice you if a tutorial is posted)
https://code.sololearn.com/WIb425hnGqfl/?ref=app
+ 1
VCoder thanks it worked, I also followed