0

I have a trouble about scope, I knew the solution but this isn't the problem.
I want to know why the member variable xhrObj unreadable from within an another member function, although that variable is global variable for that member function ?

Community
  • 1
  • 1
Lion King
  • 32,851
  • 25
  • 81
  • 143
  • It's because the value of `this` is dependent on the nature of the call to the containing function. – Pointy Jan 15 '17 at 15:12
  • `onreadystatechange` is already a member function of `xhrObj`. This means that by doing `this.xhrObj.readystate` you're trying to access `xhrObj` ON `xhrObj`. you could try to replace `this.xhrObj.readyState` with`this.readyState`, and `this.xhrObj.responseText` to `this.responseText` inside `this.xhrObj.onreadystatechange`. – JJWesterkamp Jan 15 '17 at 15:24
  • @JeffreyWesterkamp: You are right, I forgot that way. thank you. – Lion King Jan 15 '17 at 15:46

1 Answers1

1

It is happening because xhrObj function onreadystatechage is asynchrous in nature and when it return after complete call this context is different inside the onreadystatechage() and hence this.xhrObj is not different.

squiroid
  • 13,809
  • 6
  • 47
  • 67