0

As I understood "this" in Javascript should mean an object calling it, is that correct? What is "this" will mean in this example code below? `

<p id="demo"></p>

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
    myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
</script>

</body>
</html>`
Nikzin
  • 170
  • 2
  • 14

1 Answers1

2

Here this means XMLHttpRequest object which is xhttp here . But the value of this can be different based on the context . In a global execution context outside of any function this points to the window object in javascript.

Check this link for more reference :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Niladri
  • 5,278
  • 2
  • 18
  • 32