JS Crumbs —Questions around ‘this’ keyword in JavaScript

Photo by Greg Rakozy on Unsplash

A few words regarding ‘this’:

A function’s this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.
It’s value is a property of an execution context (global, function or eval) that, in non–strict mode, is always a reference to an object and in strict mode can be any value.

📓 Few pointers:

Questions❓

[Note: Imagine this code is ran on debugger console of modern browser. Also this code is not run in strict mode.]

console.log(this)function Foo () {
console.log(this);
}
Foo();
Foo.call();
Foo.apply();
var foo = new Foo();
var bar = { name: 'Bar' };
Foo(bar);
Foo.call(bar);
Foo.apply(bar);

Check the results 😏

To avoid seeing results, I’ve posted it in this bin. You can also confirm the results by pasting code in any debugger console.

Due Explanations 😅

Extras 🌻

Arigato

Thank you for finding the time to read this story. I tried to provide some good questions and explanations. Please let me know where this can be improved. Also I am thinking of taking small topics and creating short stories (2 min read) focused on each, what should I pick next?

Full Stack Developer trying to understand JavaScript and Python better