1

I'm not sure if this is a generic Javascript question: I'm using Appcelerator Titanium so there may be specifics for that platform.

Here is my question: if I add an event listener to an object, will the 'this' keyword in the event listener always point to the object?

Example:

var itemView = Ti.UI.createLabel({
    text: 'Dude'
})
itemView.addEventListener('click',function(e){
    alert(this.getText())
}) 

This example works. But maybe that's only because of some benign circumstance that doesn't always apply. My question is: can I rely on it to always work? Is the context for the event handler always the object to which the handler is bound?

Is that a fundamental Javascript law, and does Titanium / Alloy respect it?

Wytze
  • 7,844
  • 8
  • 49
  • 62

1 Answers1

0

I think you should better look at this Answer, I hope it might clear your confusion.

https://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal/134149#134149

Community
  • 1
  • 1
Yash Vekaria
  • 2,285
  • 24
  • 24
  • Thanks Yash. There's a lot of info there, but nothing specifically about 'this' inside event handlers. So I still don't know if using 'addEventListener' will always cause the this keyword inside the handler to point to the object to which the handler was attached. – Wytze Mar 25 '14 at 09:18
  • It would make sense if it did, but nobody I can find is actually saying it. – Wytze Mar 25 '14 at 09:22