Re:An Introduction to JavaScript's "this"
by mhansen on Saturday September 26, 2009 @09:50PM
(#846392)
The way I remember it: this is whatever is before the dot when you call the function.
E.g. for car.drive(); in the drive function, any reference to 'this' will get 'car'. If the function is called on it's own, as in 'drive()', 'this' will refer to the Global object (bad)
It's also possible to change 'this' by using the Function.prototype.apply() method, which allows you to pass a 'context' parameter that will become 'this'.
E.g. for car.drive(); in the drive function, any reference to 'this' will get 'car'. If the function is called on it's own, as in 'drive()', 'this' will refer to the Global object (bad)
It's also possible to change 'this' by using the Function.prototype.apply() method, which allows you to pass a 'context' parameter that will become 'this'.