DEV Community

Cover image for "this" Keyword in OOP
Deep Patel
Deep Patel

Posted on

"this" Keyword in OOP

This is a submission for DEV Computer Science Challenge v24.06.12: One Byte Explainer.

Explainer

In a bakery, each baker has their own recipe book. When a baker says "this recipe," they mean a recipe from their own book. Similarly, in programming, this refers to the current object's properties and methods, like a baker's personal recipes.

Additional Context

In simple terms, think of this as a way for an object to say "me" or "myself" to refer to its own data and functions.

  • The this keyword in object-oriented programming refers to current instance of class.

  • In a method, using this can help distinguish between class attributes and parameters with the same name.

  • It can also be used to pass the current object as a parameter to other methods or constructors.

Top comments (0)