‘this’ – part 1

Most Object Oriented programming languages have a way to refer to the current object.  In JavaScript the keyword ‘this’ is used, however it does not always return the object which is expected.

The parameter ‘this’ is one of the most widely misunderstood aspects of the language.  It can be easy to use if you think about it in the right way. It behaves almost exactly as a normal parameter, with two exceptions.

First, you are unable to pick a name, it will always be the parameter ‘this’

Second, values bound are different, compared to values bound to other parameters.

Defining the ‘this’ keyword is an identifier which gets a value bound it it.  Similar to a variable, but rather than identifying values explicitly in the code, this is bound to the correct object automatically.

The rules for how the interpreter determines the correct binding resembles the rules for positional function parameters.  The differences between positional function parameters and the ‘this’ keyword, are designed to support the coders intuition about which object should be focal when the method or constructor is evoked.

I’m going to go over this in more detail in ‘this’ part 2.  For now it good to stop and let this sink in.

Leave a Reply

Your email address will not be published. Required fields are marked *