0

This code in ES6:

var myObj = {firstname: "Stevey", 
         printFirstName() {
           console.log(this.firstname);
         }}


function printNames({printFirstName}) {
 printFirstName();
}

printNames(myObj);

TypeError: Cannot read property 'firstname' of undefined. How to fix this?

Edit: This question is not a duplicate. The code in 'exact' answer marked, does not take function from deconstructed object as function param. And the other answer marked is an essay on how 'this' works. That is just a reference not the direct answer.

  • 2
    Don't use destructuring, call the method as a method? – Bergi Apr 30 '18 at 15:48
  • 1
    *"The code in 'exact' answer marked, does not take function from deconstructed object as function param."* It doesn’t make a difference, which is why it’s a duplicate – Ry- Apr 30 '18 at 20:37
  • After reading the linked questions, do you understand how to fix this? – Bergi Apr 30 '18 at 20:40

0 Answers0