Paweł Paweł - 1 year ago 84
Javascript Question

How to bind methods of destructuring object?

How can I bind methods when I desctructure the object?

const person = {
getName: function(){

var a = person.getName;
var b = person.getName.bind(person);
var {getName:c} = person;

person.getName(); // {getName:[Function]}
a(); // window or global
b(); // {getName:[Function]}
c(); // window or global

I want
to log in the console its 'parent' object
Is there any way to bind all destructuring methods of the particular object to that object in the one (destructuring) line?

Answer Source

No, there is no way. Functions detached from objects lose the original context. And destructing in JavaScript has no syntax to do something with extracted values on the fly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download