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();
3 odpowiedzi
+ 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();
+ 2
// Try it
this.print = () => console.log( this.name + ":" , this.number);
+ 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.