Skip to content

Instantly share code, notes, and snippets.

@ionelh
ionelh / how-js-works-this-window.js
Last active August 6, 2019 19:02
Gist for medium article "How JavaScript Works"
// Global execution context (not inside a function)
this === window // logs true
@ionelh
ionelh / how-js-works-global-var.js
Last active August 6, 2019 19:02
Gist for medium article "How JavaScript Works"
foo; // 'bar'
window.foo; // 'bar'
this.foo; // 'bar'
(window.foo === foo && this.foo === foo && window.foo === this.foo) // true
@ionelh
ionelh / how-js-works-execution-stack-2.js
Last active August 6, 2019 19:14
Gist for medium article "How JavaScript Works"
function a() {
// some code
b();
// some more code
}
function b() {
// some code
}
@ionelh
ionelh / how-js-works-event-queue-1.js
Last active August 6, 2019 20:24
Gist for medium article "How JavaScript Works"
function a() {
// some code
b();
// some more code
}
function b() {
// some code
}
@ionelh
ionelh / how-js-works-scope_1.js
Last active August 6, 2019 19:00
Gist for medium article "How JavaScript Works"
function a() {
function b() {
console.log(foo); // logs 'bar'
}
var foo = 'bar';
b();
}
a();
@ionelh
ionelh / how-js-works-scope_2.js
Last active August 6, 2019 19:00
Gist for medium article "How JavaScript Works"
function a() {
var foo = 'bar';
b();
}
function b() {
console.log(foo); // throws ReferenceError: foo is not defined
}
a();
@ionelh
ionelh / how-js-works-scope_3.js
Last active August 6, 2019 19:00
Gist for medium article "How JavaScript Works"
function a() {
function b() {
function c() {
console.log(foo);
}
c();
}
var foo = 'bar';
@ionelh
ionelh / how-js-works-this_1.js
Last active August 6, 2019 18:59
Gist for medium article "How JavaScript Works"
const myArrowFunction = () => {
console.log(this === window);
};
class MyClass {
constructor() {
myArrowFunction();
}
}
@ionelh
ionelh / how-js-works-this_2.js
Last active August 6, 2019 18:58
Gist for medium article "How JavaScript Works"
class MyClass {
constructor() {
const myArrowFunction = () => {
console.log(this === window);
};
myArrowFunction();
}
}
var myClassInstance = new MyClass();
@ionelh
ionelh / how-js-works-function-declaration.js
Last active August 6, 2019 18:58
Gist for medium article "How JavaScript Works"
function a() {
// ...
}