Skip to content

Instantly share code, notes, and snippets.

@omesser
Created May 3, 2022 09:01
Show Gist options
  • Save omesser/899a5a82c6028348e2a1c26a4ed81a65 to your computer and use it in GitHub Desktop.
Save omesser/899a5a82c6028348e2a1c26a4ed81a65 to your computer and use it in GitHub Desktop.
javascript - this
/*
What will be the output of the following code?
*/
function foo() {
console.log(this);
}
const obj = {
foo: function () {
console.log(this);
}
};
class MyObject {
foo() {
console.log(this);
}
}
// 1
foo();
// 2
obj.foo();
// 3
const myObj = new MyObject();
myObj.foo();
// 4
const func = obj.foo;
func();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment