+ 3
Is the code correct
function contact(name, number) { this.name = name; this.number = number; } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print(); Any time I try making print a function it gives an error saying that print isn't a function
3 odpowiedzi
+ 4
DURU SOMTOCHUKWU PHILIP I agree with the answers Med Amine Fh and Luk gave above, but I thought I'd chip in with this. If you want to have a method called print, you can add it to the prototype of contact. Like this:
function contact(name, number) {
this.name = name;
this.number = number;
}
contact.prototype.print = function() {
console.log(`Name: ${this.name}; Number: ${this.number}`)
}
var a = new contact("David", 12345);
var b = new contact("Amy", 987654321)
a.print();
b.print();
+ 3
The contact function (constructor) don't have print method .
you just make 2 properties in that constractor (name, number).
you can access both of them like this :
for "a" instence :
a.name
a.number
for "b" instence :
b.name
b.number
+ 1
Try console.log(a).