+ 1

What's the problem with this JS code?

Contact Manager 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();

26th Sep 2021, 1:21 AM
Jonalyn E. Lawis
Jonalyn E. Lawis - avatar
8 Respostas
+ 5
In arrow function this keyword is inherited from the parent. I mean arrow function does not have its own this key word. Instead, you should use function expressions not the arrow functions. That is the reason! I hope I'm understood!😉
27th Sep 2021, 6:21 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
+ 4
Hi dear Lawis. Try this piece of code. And let me know the result🙏đŸŒčđŸŒč🙏 function contact(name, number) { this.name = name; this.number = number; this.print =function(){ var x =this.name var y =this.number console.log(x+": "+y) } } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print();
27th Sep 2021, 6:17 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
+ 1
JavaScript code
26th Sep 2021, 1:32 AM
Jonalyn E. Lawis
Jonalyn E. Lawis - avatar
+ 1
It runs fine in the code playground.
26th Sep 2021, 1:37 AM
Simon Sauter
Simon Sauter - avatar
0
Jonalyn Limentang Please specify which language in the tags of your post
26th Sep 2021, 1:31 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
Jonalyn Limentang there is a space missing from your print() func. The bit after the colon : console.log(this.name+": "+this.number);
26th Sep 2021, 1:38 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
It's ok! Can you please insert the code here?
26th Sep 2021, 4:36 AM
đŸ‡źđŸ‡± Radin Masiha đŸ‡źđŸ‡±
đŸ‡źđŸ‡± Radin Masiha đŸ‡źđŸ‡± - avatar
- 2
Can you try that this.print = bornYear function bornYear () { console.log(this.name + ':'+ this.number ); }
27th Sep 2021, 7:38 AM
Hamza Akburak
Hamza Akburak - avatar