A method destructured from an object loses its original context

My first of (hopefully) many stories where I expose how little I know about coding and share the lessons I learned (or re-learned).

Suhan Wijaya
Oct 31 · 2 min read
Programmers in 1960s writing software to fly rocket to the moon, versus programmers in 2020s struggling with VIM text editor.
Programmers in 1960s writing software to fly rocket to the moon, versus programmers in 2020s struggling with VIM text editor.
Image source: Programmer Humor

When defining a method on an object, we presumably want this to refer to the object on which the method is defined.

const someObj = {
someProp: 'someValue',
someMethod: function() {
console.log(this.someProp);
}
};

Since all the cool kids do it, I use destructuring assignments all over the place. However, destructuring a method from an object causes it to lose its original context. In other words, when invoking a destructured method, this no longer points to the object on which the method is defined.

const someObj = {
someProp: 'someValue',
someMethod: function() {
console.log(this.someProp);
}
};

Similarly, assigning a method defined on an object to a new variable, and invoking that new function variable causes it to lose its original context as well. In fact, Babel transpiles the code snippet above into the code snippet below to support the one and only browser that doesn’t support destructuring assignments.

const someObj = {
someProp: 'someValue',
someMethod: function() {
console.log(this.someProp);
}
};

Today I (re)learned.

📫 Hit me up on LinkedIn or email!

The Innovation

A place for variety of stories from different backgrounds

Sign up for The Innovation Digest

By The Innovation

Official newsletter of The Innovation Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Suhan Wijaya

Written by

Right-brained techie passionate about coding, product, UX, and fun adventures with my family. Let’s connect on linkedin.com/in/suhanwijaya

The Innovation

A place for a variety of stories from different backgrounds

Suhan Wijaya

Written by

Right-brained techie passionate about coding, product, UX, and fun adventures with my family. Let’s connect on linkedin.com/in/suhanwijaya

The Innovation

A place for a variety of stories from different backgrounds

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store