person may be made up of height, weight, gender, age, eye coloreye color = [blue, green, brown, black]this refer to and what is the advantage to using this?
This refers to dogThis allows you to use the same method definition for every object that you createdog2 and use the same method to get the dog’s name and age in human years without needing to change the codeconst dog = {
name: 'Spot',
age: 2,
color: 'white with black spots',
humanAge: function (){
console.log(`${this.name} is ${this.age*7} in human years`);
}
}
document and window objectsThis is fine for lists of a single variable like Top 10 Vacation Spots
let Top10VacationSpots = [
'Disney Land',
'New York',
'Disney World',
'etc.'
];
This is not ideal when tracking a series of values related to a specific variable
// Dog's info as an array
const dog = [
'Spot',
2,
'white with black spots',
]
// Hard to tell what each value in the array corresponds to
// Dog's info as an object
const dog = {
name: 'Spot',
age: 2,
color: 'white with black spots',
humanAge: function (){
console.log(`${this.name} is ${this.age*7} in human years`);
}
};
// Much easier to see what each value relates to through the key,value pair