Share
Embed
JS+HTML Riddles
What does this code do? Will it print anything? Or will it throw an error? Does it depend on whether it’s running in Node or in the browser?
In this talk, I’ll present a series of JavaScript and HTML riddles full of mystery, and walk through each one step by step.
(these puzzles are far more elaborate and unpredictable than the usual overused questions like [] + {} or similar examples. I dare you to solve the final riddle!)
Bruno Macabeus
January 15, 2026
Transcript
211 slides
-
✨ nothing here is meant to be production code ✨
(although I may have seen something similar in prod before 🙈) -
GAME TIME • QUESTION #1 setTimeout(() => { console.log('magic') },
Infinity) setTimeout( , Infinity) -
GAME TIME • QUESTION #1 > 'magic' is printed immediately,
only once setTimeout(() => { console.log('magic') }, Infinity) -
GAME TIME • QUESTION #1 It’s confusing because it wasn’t
well designed -
GAME TIME • QUESTION #1 The Infinity is converted to
1 (or 0) when used in the setTimeout argument, which implies running on the next tick. -
GAME TIME • QUESTION #1 But it wasn’t standardized in
the past. In old IE and Opera versions, the behavior is never calling the callback. -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } for ( ) { } for ( ) { } -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } for (var x = 0; ) { } for (let y = 0; ) { } -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } for ( ) { setTimeout(() => console.log(x)) } for ( ) { setTimeout(() => console.log(y)) } -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } -
GAME TIME • QUESTION #2 > 2 2 0 1
for (var x = 0; x < 2; x++) { setTimeout(() => console.log(x)) } for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } -
GAME TIME • QUESTION #2 It’s confusing because the for
initializer is different when using var or let -
GAME TIME • QUESTION #2 When using var, the variable
is shared for each iteration: -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 0 Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 0 Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } setTimetout(() => console.log(x)) Tasks var x = 0 Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 0 setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 1 setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 1 setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks setTimetout(() => console.log(x)) var x = 1 setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 1 setTimetout(() => console.log(x)) setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 2 setTimetout(() => console.log(x)) setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 2 setTimetout(() => console.log(x)) setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 for (var x = 0;
x < 2; x++) { setTimeout(() => console.log(x)) } Tasks var x = 2 setTimetout(() => console.log(x)) setTimetout(() => console.log(x)) Variables -
GAME TIME • QUESTION #2 When using let, a new
lexical scope is created for each iteration: -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks setTimetout(() => console.log(y_iteration_1)) let y_iteration_1 = 0 Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 let y_iteration_2 = 1 setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 let y_iteration_2 = 1 setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks setTimetout(() => console.log(y_iteration_2)) let y_iteration_1 = 0 let y_iteration_2 = 1 setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 let y_iteration_2 = 1 setTimetout(() => console.log(y_iteration_2)) setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 let y_iteration_2 = 1 let y_iteration_2 = 2 setTimetout(() => console.log(y_iteration_2)) setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for (let y = 0;
y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 let y_iteration_2 = 1 let y_iteration_2 = 2 setTimetout(() => console.log(y_iteration_2)) setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 let y_iteration_2 = 1 let
y_iteration_2 = 2 for (let y = 0; y < 2; y++) { setTimeout(() => console.log(y)) } Tasks let y_iteration_1 = 0 setTimetout(() => console.log(y_iteration_2)) setTimetout(() => console.log(y_iteration_1)) Variables -
GAME TIME • QUESTION #2 for ( let z =
{ foo: 0 }; z.foo < 2; z.foo++ ) { setTimeout(() => console.log(z.foo)) } -
GAME TIME • QUESTION #2 for ( let z =
{ foo: 0 }; z.foo < 2; z.foo++ ) { setTimeout(() => console.log(z.foo)) } // 2 2 -
GAME TIME • QUESTION #2 Let’s check how for is
de fi ned on the spec! https://262.ecma-international.org/14.0 -
GAME TIME • QUESTION #2 The spec is explicit about
the differences between var and let -
GAME TIME • QUESTION #3 > false true Object.is(0, -0)
Object.is(0n, -0n) -
GAME TIME • QUESTION #3 It’s confusing because JS has
4 different equality and uniformity algorithms -
GAME TIME • QUESTION #3 // 1. Abstract equality comparison
0 == -0 // true 0n == -0n // true // ^ this n means bigint -
GAME TIME • QUESTION #3 // 2. Strict equality comparison
0 === -0 // true 0n === -0n // true -
GAME TIME • QUESTION #3 // 3. SameValueZero [0].includes(-0) //
true [0n].includes(-0n) // true -
GAME TIME • QUESTION #3 // 4. SameValue Object.is(0, -0)
// false Object.is(0n, -0n) // true -
GAME TIME • QUESTION #3 Let’s check the SameValue on
the JS spec! https://262.ecma-international.org/14.0 -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <script> document .documentElement
.innerHTML += '<div id="magic">bar</div>' console.log(magic.innerHTML) </script> -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <script> document .documentElement
.innerHTML += '<div id="magic">bar</div>' console.log(magic.innerHTML) </script> <div id="magic">foo</div> -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <script> document .documentElement
.innerHTML += '<div id="magic">bar</div>' console.log(magic.innerHTML) </script> <script> document .documentElement .innerHTML += '<div id="magic">bar</div>' </script> -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <script> document .documentElement
.innerHTML += '<div id="magic">bar</div>' console.log(magic.innerHTML) </script> <script> console.log(magic.innerHTML) </script> -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <script> document .documentElement
.innerHTML += '<div id="magic">bar</div>' console.log(magic.innerHTML) </script> -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <script> document .documentElement
.innerHTML += '<div id="magic">bar</div>' console.log(magic.innerHTML) </script> > undefined -
GAME TIME • QUESTION #4 It’s confusing because this code
uses a code smell from a legacy and not recommended HTML feature -
GAME TIME • QUESTION #4 Let’s check it on the
HTML spec! https://html.spec.whatwg.org/ vs. -
GAME TIME • QUESTION #4 The spec de fi nes
when an element is exposed, in other words, when it’s available from window -
GAME TIME • QUESTION #4 <!-- a div with id
--> <div id="foo"></div> <script> window.foo // Element of `<div id="foo"></div>` </script> -
GAME TIME • QUESTION #4 <!-- a div with id
--> <div id="foo"></div> <script> foo // Element of `<div id="foo"></div>` </script> -
GAME TIME • QUESTION #4 <!-- an img with name
--> <img name="foo" /> <script> foo // Element of `<img name="foo" />` </script> -
GAME TIME • QUESTION #4 <!-- the same happens when
the DOM is updated --> <script> document .documentElement .innerHTML = '<div id="foo">blah</div>' foo // Element of `<div id="foo">blah</div>` </script> -
GAME TIME • QUESTION #4 <!-- and when there are
two img with same name? --> <img name="duplicated" /> <img name="duplicated" /> <script> duplicated /* HTMLCollection(2) [ img, img, duplicated: img ] */ </script> -
GAME TIME • QUESTION #4 <!-- and same id? -->
<div id="duplicated" /> <div id="duplicated" /> <script> duplicated /* HTMLCollection(2) [ div, div, duplicated: div ] */ </script> -
GAME TIME • QUESTION #4 Since HTMLCollection doesn’t have the
innerHTML property, magic.innerHTML returns undefined. -
GAME TIME • QUESTION #4 Since HTMLCollection doesn’t have the
innerHTML property, magic.innerHTML returns undefined. But, there is a bug on Firefox, and it returns the Element for the fi rst one! -
GAME TIME • QUESTION #4 <div id="magic">foo</div> <div id="magic">bar</div> <script>
magic /* It should returns HTMLCollection But, Firefox returns the Element for the first one */ </script> -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( , ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( class Magic { static console = console.log('foo') }, ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( , console.log(Magic.name) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) > it prints 'foo' and then throws in runtime -
GAME TIME • QUESTION #5 It’s confusing because the ClassExpression
and the comma operator -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( // it forces the class to become an expression ) -
GAME TIME • QUESTION #5 // ClassDeclaration: in a statement
context class Magic { } // The class keyword can be: // ClassExpression: in an expression context const M = class Magic { } ( class Magic { } ) -
GAME TIME • QUESTION #5 // ClassDeclaration: in a statement
context class Magic { } // The class keyword can be: // ClassExpression: in an expression context const M = class Magic { } ( class Magic { } ) -
GAME TIME • QUESTION #5 // ClassDeclaration: in a statement
context class Magic { } // The class keyword can be: // ClassExpression: in an expression context const M = class Magic { } ( class Magic { } ) -
GAME TIME • QUESTION #5 const M = class Magic
{ myself = () => Magic // it works } console.log(Magic) // it throws ReferenceError When using ClassExpression, the class name is only visible within the scope of the class itself -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') } console.log(Magic.name) ) , // comma operator -
GAME TIME • QUESTION #5 The comma operator receives a
list of expressions, it evaluates each item, and then returns the last one -
GAME TIME • QUESTION #5 const a = (1, 2,
3) a // 3 -
GAME TIME • QUESTION #5 ['a1', 'b2', ‘c3'].reduce( (state, pair)
=> { state[pair[0]] = pair[1] return state }, {} ) // {a: '1', b: '2', c: ‘3'} -
GAME TIME • QUESTION #5 ['a1', 'b2', ‘c3'].reduce( (state, pair)
=> { state[pair[0]] = pair[1] return state }, {} ) // {a: '1', b: '2', c: ‘3'} ['a1', 'b2', 'c3'].reduce( (state, pair) => (state[pair[0]] = pair[1], state), {} ) // {a: '1', b: '2', c: '3'} -
GAME TIME • QUESTION #5 (state, pair) => { state[pair[0]]
= pair[1] return state }, (state, pair) => (state[pair[0]] = pair[1], state), ['a1', 'b2', ‘c3'].reduce( (state, pair) => { state[pair[0]] = pair[1] return state }, {} ) // {a: '1', b: '2', c: ‘3'} ['a1', 'b2', 'c3'].reduce( (state, pair) => (state[pair[0]] = pair[1], state), {} ) // {a: '1', b: '2', c: '3'} -
GAME TIME • QUESTION #5 Normally, we don’t use the
comma operator directly. But our compilers use it frequently for two main reasons. -
GAME TIME • QUESTION #5 // 1. minify the code
if(x){foo();return bar()}else{return 1} // it’s equivalent to return x?(foo(),bar()):1 -
GAME TIME • QUESTION #5 // 2. spec complaince //
foo.ts export function foo() { return this } // index.ts import { foo } from './foo.ts' foo() // returns undefined // tsc compiles it into (0, foo()) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( class Magic { static console = console.log('foo') }, ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( , console.log( ) ) -
GAME TIME • QUESTION #5 ( class Magic { static
console = console.log('foo') }, console.log(Magic.name) ) ( , console.log(Magic.name) ) -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() this.message = 'foo' -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() class Magic { message = 'bar' say() { } start() { } } -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() class Magic { message = 'bar' say() { console.log(this.message) } start() { } } -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() class Magic { message = 'bar' say() { } start() { setTimeout(this.say, 1000); } } -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() const m = new Magic() m.start() -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() > 'foo' or undefined (depends on the env 🙃) -
GAME TIME • QUESTION #6 is too implicit and dynamic,
and the setTimeout depends on the environment It’s confusing because this -
GAME TIME • QUESTION #6 const foo = { a:
1, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 function magic(callback) { this.a =
2 callback() } foo.printA() // 1 const foo = { a: 1, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) function magic(callback)
{ this.a = 2 callback() } foo.printA() // 1 const foo = { a: 1, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) function magic(callback)
{ this.a = 2 callback() } foo.printA() // 1 const foo = { a: 1, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) function magic(callback)
{ this.a = 2 callback() } foo.printA() // 1 const foo = { a: 1, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) foo.printA() //
1 const foo = { a: 1, printA: function () { console.log(this.a) }, } function magic(callback) { this.a = 2 // `this` here is the global context callback() } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) foo.printA() //
1 const foo = { a: 1, printA: function () { console.log(this.a) }, } function magic(callback) { this.a = 2 // `this` here is the global context callback() } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) foo.printA() //
1 const foo = { a: 1, printA: function () { console.log(this.a) }, } function magic(callback) { this.a = 2 // `this` here is the global context callback() } -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) foo.printA() //
1 function magic(callback) { this.a = 2 // `this` here is the global context callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, // `this` here } // is also the // global context -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) foo.printA() //
1 function magic(callback) { this.a = 2 // `this` here is the global context callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, // `this` here } // is also the // global context // = prints `2` -
GAME TIME • QUESTION #6 magic(foo.printA) console.log(foo.a) console.log(globalThis.a) foo.printA() //
1 function magic(callback) { this.a = 2 // `this` here is the global context callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, // `this` here } // is also the // global context // = prints `2` -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 // `this` here is the global context callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, // `this` here } // is also the // global context // = prints `2` magic(foo.printA) console.log(foo.a) // 1 console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 // `this` here is the global context callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, // `this` here } // is also the // global context // = prints `2` magic(foo.printA) console.log(foo.a) // 1 console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 // `this` here is the global context callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, // `this` here } // is also the // global context // = prints `2` magic(foo.printA) console.log(foo.a) // 1 console.log(globalThis.a) // 2 -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' -
GAME TIME • QUESTION #6 foo.printA() // 1 function magic(callback)
{ this.a = 2 callback() } const foo = { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' function magic(callback) { this.a = 2 // with strict mode, `this` here is now undefined callback() } -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' function magic(callback) { this.a = 2 // undefined.a -> error! callback() } -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } magic(foo.printA) console.log(foo.a) console.log(globalThis.a) 'use strict' function magic(callback) { this.a = 2 callback() } -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } 'use strict' function magic(callback) { this.a = 2 callback() } magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } 'use strict' function magic(callback) { this.a = 2 callback() } magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } 'use strict' function magic(callback) { this.a = 2 callback() } magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 const foo
= { a: 1, printA: function () { console.log(this.a) }, } 'use strict' magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, } -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, // `this` is `foo` } -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, // `this` is `foo` } // = prints `2` -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) console.log(globalThis.a) function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, // `this` is `foo` } // = prints `2` -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, // `this` is `foo` } // = prints `2` magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) // 2 console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, // `this` is `foo` } // = prints `2` magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) // 2 console.log(globalThis.a) -
GAME TIME • QUESTION #6 foo.printA() // 1 'use strict'
function magic(callback) { this.a = 2 // `this` is `foo` callback() } const foo = { a: 1, 2, printA: function () { console.log(this.a) }, // `this` is `foo` } // = prints `2` magic.bind(foo)(foo.printA.bind(foo)) console.log(foo.a) // 2 console.log(globalThis.a) // undefined -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) // this is the global context } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 const Foo = function() {
this.a = 1 } Foo.prototype.printA = function() { console.log(this.a) // this is the global context } // = prints `undefined` function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) } } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 class Foo { constructor() {
this.a = 1 } printA() { console.log(this.a) // `this` is `undefined`; it throw error } } function magic(callback) { callback() } const f = new Foo() f.printA() // 1 magic(f.printA) -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) // but why does it not crash } // when setTimeout calls it? start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start() -
GAME TIME • QUESTION #6 There is one more prank:
what’s the environment? There are differences between the browser and Node.js including on setTimeout! -
GAME TIME • QUESTION #6 On browser, it injects the
global context on the callback. These two lines are equivalent: setTimeout(this.say, 1) setTimeout(this.say.bind(window), 1) -
GAME TIME • QUESTION #6 On Node, the this from
the setTimeout callback is the instance from the Timeout class. And since there is no message property on this object, this.message is undefined -
GAME TIME • QUESTION #6 this.message = 'foo' class Magic
{ message = 'bar' say() { console.log(this.message) } start() { setTimeout(this.say, 1000); } } const m = new Magic() m.start()