Uno de los grandes misterios de ECMAScript desvelado 🙂
We said earlier that
thisis not an author-time binding but a runtime binding. It is contextual based on the conditions of the function’s invocation.
thisbinding has nothing to do with where a function is declared, but has instead everything to do with the manner in which the function is called.
When a function is invoked, an activation record, otherwise known as an execution context, is created. This record contains information about where the function was called from (the call-stack), how the function was invoked, what parameters were passed, etc. One of the properties of this record is the
thisreference which will be used for the duration of that function’s execution.
You Don’t Know JS: this & Object Prototypes — Leslie Zhang
Y qué decir de los libros de la serie You don’t know JS: muy recomendables.