Skip to main content
added 326 characters in body
Source Link
Duc Filan
  • 7.2k
  • 3
  • 24
  • 26

In your code:

const name = { firstName: 'John', lastName: 'Doe', fullName: function() { return this.firstName + ' ' + this.lastName; } }; 

this is not refered to your variable name anymore. To solve, you need to bind this back to the name you declared:

formatname(name).bind(name)() 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

In your code:

const name = { firstName: 'John', lastName: 'Doe', fullName: function() { return this.firstName + ' ' + this.lastName; } }; 

this is not refered to your variable name anymore. To solve, you need to bind this back to the name you declared:

formatname(name).bind(name)() 

In your code:

const name = { firstName: 'John', lastName: 'Doe', fullName: function() { return this.firstName + ' ' + this.lastName; } }; 

this is not refered to your variable name anymore. To solve, you need to bind this back to the name you declared:

formatname(name).bind(name)() 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Source Link
Duc Filan
  • 7.2k
  • 3
  • 24
  • 26

In your code:

const name = { firstName: 'John', lastName: 'Doe', fullName: function() { return this.firstName + ' ' + this.lastName; } }; 

this is not refered to your variable name anymore. To solve, you need to bind this back to the name you declared:

formatname(name).bind(name)()