This Keyword - Functions Flashcards
What is the value of ‘this’ and why?:
function testMe() {
console.log(this);
}
testMe();
GLOBAL/WINDOW
Because:
1. This is a STANDARD function.
2. The value of ‘this’ is set when the function is INVOKED..
3. The value of ‘this’ is set to the same value of ‘this’ defined in the lexical scope that it was invoked in.
4. In this case the lexical scope is the GLOBAL/WINDOW object.
What is the value of ‘this’ and why?:
function testMe() {
((a, b) => {
console.log(this);
})(2, 3);
}
testMe();
GLOBAL/WINDOW
Because:
1. The outer function is a STANDARD function..
2. The value of ‘this’ is set when the function is INVOKED.
3. The value of ‘this’ is set to the same value of ‘this’ defined in the lexical scope that it was invoked in. In this case the lexical scope is the GLOBAL/WINDOW object.
4. The inner ARROW function inherits the value of ‘this’ from the execution context that it was DEFINED in.
5. The execution context (after invocation) of the outer function is GLOBAL/WINDOW therefore the value of ‘this’ inside the arrow function is GLOBAL/WINDOW.
What is the value of ‘this’ and why?:
let me = function () {
console.log(this);
};
function testMe() {
me();
}
testMe();
GLOBAL/WINDOW
Because:
1. The outer function is a STANDARD function..
2. The value of ‘this’ is set when the function is INVOKED.
3. The value of ‘this’ is set to the same value of ‘this’ defined in the lexical scope that it was invoked in. In this case the lexical scope is the GLOBAL/WINDOW object.
4. The inner standard function inherits the value of ‘this’ from the execution context that it was invoked in.
5. The fact that it was defined outside in the global space makes no difference.
6. The value of ‘this’ is set to the same value of ‘this’ defined in the lexical scope that it was invoked in.
7. In this case the lexical scope is the GLOBAL/WINDOW object.
What is the value of ‘this’ and why?:
function TestMe() {
this.arrow = “Yes”;
return () => {
console.log(this);
};
}
let newItem = new TestMe();
newItem();
TestMe
Because:
1. The outer function has a NEW constructor which creates user-defined object type AND sets the value of ‘this’ to the object.
2. The value of ‘this’ is set to the object.
3. The inner ARROW function inherits the value of ‘this’ from the execution context that it was DEFINED in.
4. Even though the arrow function is finally invoked on the window/global it was DEFINED in the TestMe object
5. Therefore the value of ‘this’ is the TestMe function/object
What is the value of ‘this’ and why?:
function testMe() {
return function () {
console.log(this);
};
}
let newItem = new testMe();
newItem();
GLOBAL/WINDOW
Because:
1. The outer function has a NEW constructor which creates user-defined object type AND sets the value of ‘this’ to the object
2. The value of ‘this’ is set to the object.
3. The inner standard function inherits the value of ‘this’ from the execution context that it was INVOKED in.
4. The function is INVOKED in the GLOBAL/WINDOW
5. Therefore the value of ‘this’ is the TestMe GLOBAL/WINDOW
What is the value of ‘this’ and why?:
function TestMe() {
this.arrow = “Yes”;
setTimeout(function () {
console.log(this);
}, 1000);
}
new TestMe(); // constructor
GLOBAL/WINDOW
Because:
1. The outer function has a NEW constructor which creates user-defined object type.
2. The value of ‘this’ is set to the object.
3. However the inner callback is a standard function.
4. The setTimeout function has a GLOBAL/WINDOW execution context.
4. The function will be INVOKED in the the setTimeout execution context and inherits the value of this which is GLOBAL/WINDOW
What is the value of ‘this’ and why?:
function TestMe() {
this.arrow = “Yes”;
setTimeout(() => {
console.log(this);
}, 1000);
}
new TestMe(); // constructor
TestMe
- The outer function has a NEW constructor which creates user-defined object type.
- The value of ‘this’ is set to the object.
- However the inner callback is an ARROW function.
- The inner ARROW function inherits the value of ‘this’ from the execution context that it was DEFINED in.
- Therefore the value of ‘this’ is the TestMe function/object
What is the output and why?:
function User() {
this.name = “John Doe”;
this.sayUser = function () { console.log(this.name); function innerFunction() { console.log(this.name); } innerFunction(); }; } let name = new User(); name.sayUser();
John Doe
Undefined
- The outer function has a NEW constructor which creates user-defined object type
- The value of ‘this’ is set to the object.
- The function is INVOKED on the User object (labelled name)
- The value of this is the object and the name is a property on that object
- Therefore John Doe is logged.
- The inner function is NOT attached to the object, and is a global function.
- The inner function is INVOKED and the this is set to the WINDOW/GLOBAL object
- The global object does not have a name property therefore is UNDEFINED.
What is the output and why?:
function User() {
this.name = “John Doe”;
this.sayUser = function () { console.log(this.name); let innerFunction = () => { console.log(this.name); }; innerFunction(); }; } let name = new User(); name.sayUser();
John Doe
John Doe
- The outer function has a NEW constructor which creates user-defined object type
- The value of ‘this’ is set to the object.
- The function is INVOKED on the User object (labelled name)
- The value of this is the object and the name is a property on that object.
- Therefore John Doe is logged.
- The inner function is an ARROW function
- The arrow function inherits the value of this from the containing object
- this.name is defined inside the arrow function and therefore we get a second John Doe
Set timeout without wrapper function.
Where do arrow functions get the value of this?
From the enclosing lexical context. 
const myObject = {
myMethod(items) {
console.log(this);
const callback = () => {
console.log(this);
};
items.forEach(callback);
}
};
myObject.myMethod([1, 2, 3]);