Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Libor Gess
Libor Gess
6,880 Points

Why the teacher started talk about ob programming?

I do not understand the code of the example. I do know how to write the arrow functions but why the teacher showing this example and talking about OB programming?

var Person = function (data) { for (var key in data) { this[key] = data[key];
} this.getKeys = () => Object.keys(this);

}

var Alena = new Person({ name: 'Alena', role: 'Teacher' });

console.log('Alena\'s Keys:', Alena.getKeys()); // 'this' refers to 'Alena'

var getKeys = Alena.getKeys;

console.log(getKeys()); // 'this' refers to the node process

I do not understand and I find this example completely confusing and irritable. Also what was the purpose of the example? Why I should still pay for this context?

1 Answer

Steven Parker
Steven Parker
231,269 Points

I think the point of the demonstration is to show how the keyword "this" has different meanings when used in different contexts. Also how conventional functions create "this" within their scope, but arrow functions do not.