For each exercise try to guess the output. What this
points to, and more important, why? (Assuming the code is running in a browser).
Ex. #1:
function outer() {
const arrow = () => console.log(this);
arrow();
}
outer();
Ex. #2:
function outer() {
const obj = {
init: () => console.log(this)
};
obj.init();
}
outer();
Ex. #3:
const obj = {
nested: {
init: () => console.log(this)
}
};
obj.nested.init();
Ex. #4:
const object = {
init: function() {
(() => console.log(this))();
}
};
object.init();
Ex. #5:
const object = {
init: function() {
setTimeout(function() {
const arrow = () => console.log(this);
arrow();
}, 5000);
}
};
object.init();
Ex. #6:
const object = {
init: function() {
setTimeout(function() {
fetch("https://jsonplaceholder.typicode.com/todos/").then(function() {
const arrow = () => console.log(this);
arrow();
});
}, 5000);
}
};
object.init();
Ex. #7:
const object = {
init: function() {
setTimeout(function() {
const object = {
whoIsThis: function() {
console.log(this);
}
};
object.whoIsThis();
}, 5000);
}
};
object.init();
Put your solutions in the comments below!