Master the "this" keyword Flashcards

(31 cards)

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Context: Node.js, non-strict

Code:

```js
console.log(“Top-level:”, this);
function test() {
console.log(“Inside test:”, this);
}
test();
~~~

Options:
A. Top-level: [object global]
Inside test: [object global]
B. Top-level: {}
Inside test: global
C. Top-level: {}
Inside test: [object global]
D. Top-level: undefined
Inside test: undefined

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Context: Browser, non-strict

Code:
js

<button id="myBtn" onclick="console.log(this)">Click me</button>

Options:
A. The window object
B. The <button> DOM element
C. undefined
D. null</button>

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Context: Node.js, non-strict

Code:

```js
const obj = {
name: “NodeTest”,
show: () => {
console.log(this.name);
}
};
obj.show();
~~~

Options:
A. “NodeTest”
B. undefined
C. Throws an error
D. this.name

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Context: Browser, strict

Code:

```js
“use strict”;
function testStrict() {
console.log(this);
}
testStrict();
~~~

Options:
A. window
B. undefined
C. {}
D. null

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Context: Node.js, non-strict

Code:

```js
function Outer() {
function Inner() {
console.log(this);
}
Inner();
}
Outer();
~~~

Options:
A. undefined
B. The global object
C. The Outer function’s context
D. Throws an error

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Context: Browser, non-strict

Code:

```js
const obj = {
value: 42,
getValue: function () {
return () => {
console.log(this.value);
};
}
};
const fn = obj.getValue();
fn();
~~~

Options:
A. 42
B. undefined
C. null
D. Throws an error

A

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Context: Browser, script

Code:

```js
console.log(this);
~~~

Options:
A. undefined
B. window
C. {}
D. The global object (but not window)

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Context: Node.js, strict

Code:

```js
“use strict”;
function logThis() {
console.log(this);
}
logThis();
~~~

Options:
A. {}
B. global
C. undefined
D. null

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Context: Node.js, ES module

Code:

```js
console.log(this);
~~~

Options:
A. undefined
B. global
C. window
D. {}

A

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Context: Browser, non-strict

Code:

```js
function sayHi() {
console.log(this.name);
}
let name = “Global”;
const person = {
name: “Alice”,
sayHi: sayHi
};
const ref = person.sayHi;
ref();
~~~

Options:
A. Alice
B. Global
C. undefined
D. Throws a ReferenceError

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Context: Node.js, non-strict

Code:

```js
function MyClass() {
this.value = 100;
return {
value: 200
};
}
const instance = new MyClass();
console.log(instance.value);
~~~

Options:
A. 100
B. 200
C. undefined
D. Throws an error

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Context: Node.js, strict

Code:

```js
“use strict”;
const obj = {
val: 10,
method() {
function inner() {
console.log(this.val);
}
inner();
}
};
obj.method();
~~~

Options:
A. 10
B. undefined
C. Throws an error
D. null

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Context: Node.js, non-strict

Code:

```js
const obj = {
a: 1,
b: function () {
return (() => {
console.log(this.a);
});
}
};
const fn = obj.b();
fn();
~~~

Options:
A. 1
B. undefined
C. Throws an error
D. null

A

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Context: Browser, strict

Code:

```js
“use strict”;
const obj = {
name: “HTML”,
show: function() {
setTimeout(function() {
console.log(this.name);
}, 0);
}
};
obj.show();
~~~

Options:
A. HTML
B. undefined
C. null
D. window

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Context: Browser, non-strict

Code:

```js
const obj = {
name: “clicky”,
handleClick: function () {
document.body.addEventListener(“click”, function () {
console.log(this.name);
});
}
};
obj.handleClick();
~~~

Options:
A. clicky
B. undefined
C. body
D. document

17
Q

Context: Node.js, strict

Code:

```js
“use strict”;
class Counter {
constructor() {
this.count = 0;
}

increment() {
function step() {
this.count++;
}
step();
}
}
const c = new Counter();
c.increment();
console.log(c.count);
~~~

Options:
A. 1
B. undefined
C. 0
D. Throws a TypeError

18
Q

Context: Browser, module

Code:

```js
console.log(this);
~~~

Options:
A. undefined
B. window
C. globalThis
D. {}

19
Q

Context: Node.js, strict

Code:

```js
const person = {
name: “Tom”,
greet: () => {
console.log(Hello, ${this.name});
}
};
person.greet();
~~~

Options:
A. Hello, Tom
B. Hello, undefined
C. Throws a TypeError
D. Hello, this.name

20
Q

Context: Browser, non-strict

Code:

```js
var length = 4;
function callback() {
console.log(this.length);
}
const object = {
length: 5,
method: function () {
arguments0;
}
};
object.method(callback, 2, 3);
~~~

Options:
A. 5
B. 3
C. 4
D. undefined

21
Q

Context: Node.js, non-strict

Code:

```js
const obj = {
value: 42,
display: function() {
const inner = {
value: 100,
method: () => {
console.log(this.value);
}
};
inner.method();
}
};
obj.display();
~~~

Options:
A. 42
B. 100
C. undefined
D. Throws an error

22
Q

Context: Browser, non-strict

Code:

```js
var length = 10;
function fn() {
console.log(this.length);
}
const obj = {
length: 5,
method: function () {
arguments0;
}
};
obj.method(fn, 1, 2);
~~~

Options:
A. 10
B. 5
C. 3
D. undefined

23
Q

Context: Browser, non-strict

Code:

```js
const obj = {
name: “Alice”,
greet: function () {
return function () {
console.log(this.name);
};
}
};
const fn = obj.greet();
fn();
~~~

Options:
A. Alice
B. undefined
C. Throws a TypeError
D. window

24
Q

Context: Node.js, strict

Code:

```js
“use strict”;
const obj = {
name: “Node”,
getName: function () {
return () => this.name;
}
};
const other = { name: “Other” };
const fn = obj.getName();
console.log(fn.call(other));
~~~

Options:
A. Other
B. Node
C. undefined
D. Throws a TypeError

25
Context: Browser, script Code: ```js setTimeout(function() { console.log(this); }, 0); ``` Options: A. undefined B. window C. setTimeout object D. globalThis
B
26
Context: Browser, module Code: ```js console.log(this); ``` Options: A. window B. undefined C. {} D. globalThis
B
27
Context: Node.js, strict Code: ```js "use strict"; class Base { constructor() { this.value = 1; } get getValue() { return function () { return this.value; }; } } const obj = new Base(); const fn = obj.getValue; console.log(fn()); ``` Options: A. 1 B. undefined C. Throws a TypeError D. null
C
28
Context: Node.js, non-strict Code: ```js function test() { console.log(this); } test.call(null); ``` Options: A. null B. global C. undefined D. Throws a TypeError
B
29
Context: Node.js, non-strict Code: ```js const obj1 = { val: 1, show: function () { console.log(this.val); } }; const obj2 = { val: 2, show: obj1.show }; const fn = obj2.show; fn(); ``` Options: A. 1 B. 2 C. undefined D. Throws an error
C
30
Context: Browser, script Code: ```js const obj = { value: 10, method: function () { return this.value; } }; const other = { value: 42 }; other.method = obj.method.bind(obj); console.log(other.method()); ``` Options: A. 10 B. 42 C. undefined D. Throws a TypeError
A
31
Context: Browser, non-strict Code: ```js const a = { foo: function () { console.log(this.bar); }, bar: 1 }; const b = { bar: 2 }; (b.foo = a.foo)(); ``` Options: A. 1 B. 2 C. undefined D. Throws a TypeError
C