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 Introducing ES2015 The Cooler Parts of ES2015 Arrow Functions

TzeYang Chew
TzeYang Chew
12,039 Points

hi can anyone explain why this set of code return Alena's Keys: [ 'name', 'role', 'getKeys' ]?

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());
Steven Parker
Steven Parker
229,732 Points

I don't quite understand what you are asking. "[ 'name', 'role', 'getKeys' ]" does not appear in your sample code, and it would just be an array of literal strings if it did.

Can you elaborate a bit more?

TzeYang Chew
TzeYang Chew
12,039 Points

when i log in to my terminal for this file, it shows this code which i dont understand ..Alena's Keys: [ 'name', 'role', 'getKeys' ]

1 Answer

Steven Parker
Steven Parker
229,732 Points

I understand now. That's the output you see from the "console.log" at the end of the script.

The function "Alena.getKeys()" is being called, and the output that it gives back is "["name", "role", "getKeys"]".

The purpose of this function is to show you all the "keys" (or "names") of the object properties. And since "Alena" is an instance of a "Person" object, it has 3 properties, one to hold the person's name (called "name"), one to hold the person's job (called "role"), and finally the "getKeys" function itself. So those are the 3 things it returns.

Does that clear it up?

TzeYang Chew
TzeYang Chew
12,039 Points

Hi i have a few question to ask here.. is name: - object or properties? 'Alena' - object or properties? var Alena = new Person({ name: 'Alena', role: 'Teacher' });

what is the data here means? var Person = function (data) {

this line of code i find it hard to understand this[key] = data[key];

Steven Parker
Steven Parker
229,732 Points
  • "name" is one of the properties, so it is also one of the keys (keys are the names of the properties)
  • "Alena" is an object, and an instance of the "Person" class
  • "data" is the parameter which represents the object that will be passed in when an instance is created
  • "this[key] = data[key]" takes each key (property) from the data object and copies it into the new "Person" object that is being constructed ("this").
TzeYang Chew
TzeYang Chew
12,039 Points

var string1 = ""; var object1 = {a: 1, b: 2, c: 3};

for (var property1 in object1) { string1 = string1 + object1[property1]; }

console.log(string1); // expected output: "123"

Hi, when i look at the above mdn code i'm confuse as why it doesn't it show this output - Alena's Keys: [ 'Alena', 'Teacher', 'getKeys' ] ?

Steven Parker
Steven Parker
229,732 Points

The difference is in the new code you have "object1[property1]" which uses the key ("property1") to get the associated value.

The original code does not get the value but returns the keys themselves. That's why it does not show "Alena" or "Teacher", those are both values. But the keys that go with them are "name" and "role".