You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -876,3 +876,24 @@ console.log(text);
876
876
</details>
877
877
878
878
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
879
+
880
+
```js
881
+
constuser= {
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)**
0 commit comments