Skip to content

Commit 00b2014

Browse files
authored
Merge pull request #5 from amanbhoria/master
Added a question
2 parents fafcf3c + 4515596 commit 00b2014

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,24 @@ console.log(text);
876876
</details>
877877

878878
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
879+
880+
```js
881+
const user = {
882+
name: 'Aman Bhoria!',
883+
logMessage() {
884+
console.log(this.name); // What is logged?
885+
}
886+
};
887+
setTimeout(user.logMessage, 1000);
888+
```
889+
<details>
890+
<summary><b>View Answer</b></summary>
891+
<ul>
892+
<li><b>Output</b> : This is undefined</li>
893+
<li><b>Reason</b> : We've passed the reference in setTimeout not the actual function so as a result it doesn't have the user's context while executing. To get the name we've to pass a callback like:
894+
setTimeout(() => user.logMessage(), 1000); </li>
895+
</ul>
896+
</details>
897+
898+
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
899+

0 commit comments

Comments
 (0)