Javascript Challenge 1
# Javascript Challenges ## Challenge 1 - Try to guess what the below code prints. - Fix the code ``` let person = { name: 'John Doe', getName: function() { console.log(this.name); } }; setTimeout(person.getName, 1000); ``` Lets do some analysis ### When Method is in an Object: - The method `getName` is initially defined as part of the `person` object. - When you call `person.getName()`, the `this` keyword refers to the `person` object. So `this.name` would correctly return 'John Doe'. ### The Issue with `setTimeout` - However, when a function is passed as a callback (like `person.getName` in this case), it loses its context (i.e., the reference to the person object). - Instead, the value of `this` inside `getName` changes to the global object (window in browsers, or global in Node.js). - In non-strict mode, the global object (`window `in browsers) doesn't have a name property, so `this.name` becomes `undefined`. ### How `this` changes in `setTi...