Master the "this" keyword Flashcards
(31 cards)
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
C
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>
B
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
B
Context: Browser, strict
Code:
```js
“use strict”;
function testStrict() {
console.log(this);
}
testStrict();
~~~
Options:
A. window
B. undefined
C. {}
D. null
B
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
B
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
Context: Browser, script
Code:
```js
console.log(this);
~~~
Options:
A. undefined
B. window
C. {}
D. The global object (but not window
)
B
Context: Node.js, strict
Code:
```js
“use strict”;
function logThis() {
console.log(this);
}
logThis();
~~~
Options:
A. {}
B. global
C. undefined
D. null
C
Context: Node.js, ES module
Code:
```js
console.log(this);
~~~
Options:
A. undefined
B. global
C. window
D. {}
A
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
B
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
B
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
C
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
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
B
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
B
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
D
Context: Browser, module
Code:
```js
console.log(this);
~~~
Options:
A. undefined
B. window
C. globalThis
D. {}
A
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
B
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
B
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
A
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
C
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
B
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
B