Complete the code to log the value of this inside the arrow function.
const obj = {
name: 'Alice',
greet: () => {
console.log([1]);
}
};
obj.greet();Arrow functions do not have their own this. They use the this from the surrounding scope, which here is the global or module scope.
Complete the code to correctly access the object's name property inside a regular function.
const obj = {
name: 'Bob',
greet: function() {
console.log(this[1]);
}
};
obj.greet();Inside a regular function, this refers to the object calling the function. Use .name to access the property.
Fix the error in the code to make this inside the arrow function refer to the object.
const obj = {
name: 'Carol',
greet: function() {
const sayName = () => {
console.log(this[1]);
};
sayName();
}
};
obj.greet();The arrow function inherits this from the greet method, which refers to obj. Use .name to access the property.
Fill both blanks to create an object method using an arrow function that logs a property of the global this and the object's name property.
const obj = {
name: 'Dave',
greet: () => {
console.log(this[1]);
console.log(obj[2]);
}
};
obj.greet();this.name inside the arrow function expecting it to refer to the object.The arrow function's this refers to the global object, which has a location property. To get the object's name, access obj.name.
Fill the blanks to create a method that uses a regular function and an arrow function to log the object's name property from both.
const obj = {
name: 'Eve',
greet: function() {
const arrowFunc = () => console.log(this[1]);
console.log(this[2]);
arrowFunc();
}
};
obj.greet();this refers to the object or global scope.The greet regular function has this referring to obj, so this.name logs 'Eve'. The nested arrow function inherits this same this from its lexical environment, also logging 'Eve'.