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 trialella sherilyn ramos
7,063 PointsWhat is the reason for assigning getKeys to another variable?
Hello,
I would just like to understand why we are trying to assign the getKeys to another variable? How is this relatable in real world problem?
And doing this returns the same set of keys.
'use strict';
var Person = function (data) {
for (var key in data) {
this[key] = data[key];
}
this.getKeys = function () {
return 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
3 Answers
Steven Parker
231,248 PointsThe last 2 lines are a bit different in the video. They look like this instead:
var getKeys = Alena.getKeys;
console.log(getKeys()); // 'this' refers to the node process
As shown in the video, these produce different results.
Libor Gess
6,880 PointsI do not understand the purpose of the example. Why we just into oriental object programming?
Also why some people not answer question correctly? The question was simple 'why we are trying to assign the getKeys to another variable? How is this relatable in real world problem?'
Miguel Barra
6,855 PointsThe purpose of this example is to explain how anonymous functions and arrow functions work, is not focus on object-oriented programming. First, Object.keys() is a JavaScript method, please refer to this URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys This object receives an object as an argument, in the example we pass this as the argument to the function where this is the actual Person object (Elena). Now, when we assign Alena.getKeys to the getKeys variable the this scope out of the object and refers to the top-level code in a Node module, this is equivalent to module.exports. (that's because we run the file with node if we run it with chrome would be another story). With the arrow function, the function keeps referring to the object. That's the advantage of arrow function above anonymous functions which the teacher tries to explain.
ella sherilyn ramos
7,063 Pointsella sherilyn ramos
7,063 Pointsi know it has different lines of code. I just want to know why they did that. They could have provided a better example.
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThe point of the demonstration is to show how "this" has different meanings when used in different contexts.