if we call the getName function in an sebuahobjeklain, we will get an undefined output. What's wrong ? And how to fix it ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

if we call the getName function in an sebuahobjeklain, we will get an undefined output. What's wrong ? And how to fix it ?

const sebuahObjectLain = { name: 'Bejo Jhonson', getName: () => this.name }

16th Jun 2020, 8:43 PM
Febriantika Triarini
3 Answers
+ 5
We cannot use 'this' when using arrow functions. Solutions: const sebuahObjectLain = { name: 'Bejo Jhonson', getName: function(){ return this.name; } } //Ecmascript6: const sebuahObjectLain = { name: 'Bejo Jhonson', getName(){ return this.name; } }
16th Jun 2020, 11:03 PM
CoffeeByte
CoffeeByte - avatar
+ 2
Hi post your code in description part not in question title ,it has limited words and therefore your code is cut
16th Jun 2020, 8:54 PM
Abhay
Abhay - avatar
+ 1
Febriantika Triarini When using this in arrow function, this refeers to global object. Here you have not defined name in the global object.
17th Jun 2020, 12:16 AM
EmmanueLZ.
EmmanueLZ. - avatar