person with a property name set to 'Alice'.sayHelloRegular using a regular function that returns a greeting using this.name.sayHelloArrow using an arrow function that returns a greeting using this.name.this behavior.person with a property name set to 'Alice'.sayHelloRegular using a regular function that returns a greeting using this.name.sayHelloArrow using an arrow function that returns a greeting using this.name.this behavior.person object with a name propertyperson with a property name set to the string 'Alice'.Use const person = { name: 'Alice' } to create the object.
sayHelloRegularsayHelloRegular to the person object using a regular function syntax. This method should return the string `Hello, my name is ${this.name}` using a template literal.Use sayHelloRegular: function() { return `Hello, my name is ${this.name}`; }.
sayHelloArrowsayHelloArrow to the person object using an arrow function syntax. This method should return the string `Hello, my name is ${this.name}` using a template literal.Use sayHelloArrow: () => { return `Hello, my name is ${this.name}`; }.
console.log statements to print the results of calling person.sayHelloRegular() and person.sayHelloArrow().Use console.log(person.sayHelloRegular()) and console.log(person.sayHelloArrow()).
What is true about this in arrow functions compared to regular functions?
this works in arrow functionsthis. Instead, they use the this value from the surrounding (lexical) context where they are created.this to the global object, undefined, or themselves.this in arrow functions is inherited from the surrounding code where they are created. -> Option Athis in arrow functions = lexical this [OK]this, not their own [OK]thisthis is always global in arrow functionsWhich of the following is the correct syntax to define an arrow function that logs this.name?
const obj = { name: 'Alice', logName: ??? }() => expression or () => { statements }. logName: () => console.log(this.name) uses the correct concise syntax.function() syntax, not arrow functions. logName: () { console.log(this.name) } () { console.log(this.name) } is missing the fat arrow =>, resulting in a syntax error. Note: even with correct arrow syntax as an object method, this would refer to the lexical scope (global), not the object.What will be the output of this code?
const person = {
name: 'Bob',
greet: () => {
console.log(this.name);
}
};
person.greet();this in arrow functions inside objectsgreet does not have its own this. It uses this from the surrounding scope, which is the global scope here.this.namethis.name in global scope is undefined unless explicitly set.this = lexical this (global) [OK]this, not object [OK]this to be the objectthis.name is setFind the error in this code and fix it so that this.name inside greet refers to the user object:
const user = {
name: 'Carol',
greet: () => {
console.log(this.name);
}
};
user.greet();this.name is incorrectthis, so inside greet, this is not the user object.greet as a regular function method binds this to the user object when called as user.greet().greet to a regular function: greet() { console.log(this.name); } -> Option Cthis [OK]this [OK]this to arrow functionsthisthisYou want to create an object with a method that uses a callback to print the object's name property after 1 second. Which code correctly uses arrow functions to keep this referring to the object?
const obj = {
name: 'Dave',
printName: function() {
setTimeout(??? , 1000);
}
};
obj.printName();this inside setTimeout callbacksetTimeout, a regular function's this is global or undefined, so it loses the object context.this from printNamethis from printName, which is the object obj.this from method [OK]this context [OK]this contextthisPass the test and this pattern moves unknown