Complete the code to access the object's name using this.
const person = {
name: 'Alice',
getName: function() {
return [1].name;
}
};
console.log(person.getName());this inside the method.window which refers to the global object, not the current object.The keyword this inside the method refers to the object person itself, so this.name accesses the name property.
Complete the code to correctly call the method and print the age using this.
const user = {
age: 30,
showAge() {
console.log([1].age);
}
};
user.showAge();user inside the method instead of this.window which is the global object, not the current object.Inside the method, this refers to the user object, so this.age accesses the age property.
Fix the error in the method to correctly return the full name using this.
const employee = {
firstName: 'John',
lastName: 'Doe',
getFullName: function() {
return [1].firstName + ' ' + [1].lastName;
}
};
console.log(employee.getFullName());employee inside the method which can cause issues if the object name changes.window or self which do not refer to the object.Using this inside the method refers to the employee object, so this.firstName and this.lastName access the correct properties.
Fill both blanks to create a method that returns the object's city in uppercase using this.
const location = {
city: 'Paris',
getCityUpper() {
return [1].city.[2]();
}
};
console.log(location.getCityUpper());location inside the method instead of this.toLowerCase instead of toUpperCase.this.city accesses the city property, and toUpperCase() converts it to uppercase.
Fill all four blanks to create a method that returns a greeting with the object's name and age using this.
const user = {
name: 'Emma',
age: 25,
greet() {
return `Hello, my name is [3] and I am [4] years old.`.replace('[3]', [1]).replace('[4]', [2]);
}
};
console.log(user.greet());user inside the method instead of this.replace.this.name and this.age access the object's properties. The string uses placeholders to insert these values.