this (computer programming)
In many object-oriented programming languages, this
(also called self
or Me
) is a keyword that is used in instance methods to refer to the object on which they are working. C++ and languages which derive in style from it (such as Java, C#, and PHP) generally use this
. Smalltalk and others such as Object Pascal, Python, Ruby, and Objective-C use self
; Visual Basic uses Me
.
The concept is similar in all languages. For brevity, here we just say this
.
this
is usually an immutable reference or pointer which refers to the current object. Some languages, such as Objective-C, allow assignment to this
. Doing so can be very misleading to maintenance programmers, because the assignment does not modify the original object, only changing which object that the rest of the code in the method refers to, and can end with undefined behavior.[citation needed]
After an object is properly constructed this
is always a valid reference. Some languages require it explicitly; others use lexical scoping to use it implicitly to bring symbols within their class visible. In the latter case, the use of this
in code, while not illegal, may raise warning bells to a maintenance programmer, although there are still legitimate uses of this
in this case, such as referring to instance variables hidden by local variables of the same name, or if the method wants to return a reference to the current object, i.e. this
, itself.
this
becomes an extra parameter to an instance method. For example, the following instance method in C++
int foo::print (bar x)
is essentially equivalent to the procedural programming:
int foo_print (foo *const this, bar x)
In some languages, for example Python and Perl 5, this
is made explicit, the first parameter of an instance method being such a reference. It needs explicitly to be specified. In this case, the parameter need not necessarily be named this
or self
; like any other parameter, it can be freely named by the programmer; however, by informal convention, the first parameter of an instance method in Perl and Python is named self
.
In some compilers (for example GCC), pointers to C++ instance methods can be directly cast to a pointer of another type, with an explicit this
pointer parameter.[1]
Static methods in C++ or Java are not associated with instances but classes, and so cannot use this
, because there is no object. In others, such as Python, Ruby, Smalltalk or Objective-C, the method is associated with a class object that is passed as this
, and are called class methods.
Contents |
Implementations
C++
Early versions of C++ would let the this
pointer be changed; by doing so a programmer could change which object a method was working on. This feature was eventually removed, and now this
in C++ is an r-value.[2]
Early versions of C++ did not include references and it has been suggested that had they been so in C++ from the beginning, this
would have been a reference, not a pointer.[3]
C++ lets objects destroy themselves with the idiom delete this
.
C#
this
in C# works the same way as in Java, for reference types. However, within C# "value types", this
has quite different semantics, being similar to an ordinary mutable variable reference, and can even occur on the left side of an assignment.
Dylan
In Dylan, an OO language that supports multimethods and hasn't a concept of this
, sending a message to an object is still kept in the syntax. The two forms below work the same; the differences are just syntactic sugar.
object.method(param1, param2)
and
method (object, param1, param2)
Java
A Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods.
Since all instance methods are virtual in Java, this
can never be null.
"...the reason for using this construct [this] is that we have a situation that is known as name overloading - the same name being used for two different entities....It is important to understand that the fields and the parameters are separate variables that exist independently of each other, even though they share similar names. A parameter and a field sharing a name is not really a problem in Java." - Objects First with Java, D. Barnes and M. Kölling
JavaScript
this
is a keyword in JavaScript. What it evaluates to depends on where it is used.
- When used outside any function,
this
refers to the global object (e.g.window
in browsers) - When used in a function, what
this
is depends on how the function is called- When a function is called directly (e.g.
f(x)
),this
refers to the global object (or in strict mode, it isundefined
) - When a function is called as a method of an object (e.g.
obj.f(x)
),this
is set to the object that it is called on - It is possible to manually specify a custom value as the
this
when calling the function, by using the.call()
or.apply()
methods of the function object to call the function. For example, the method callobj.f(x)
could also be written asobj.f.call(obj, x)
- When a function is called directly (e.g.
Python
In Python, there is no keyword for this
. When a member function is called on an object, it invokes the member function with the same name on the object's class object, with the object automatically bound to the first argument of the function. Thus, the obligatory first parameter of "instance methods" serves as this
; this parameter is conventionally named self
, but can be named anything.
In class methods (created with the classmethod
decorator), the first argument refers to the class object itself. In static methods (created with the staticmethod
decorator), no special first argument exists.
Self
The Self language is named after this use of "self".
Xbase++
Self
is strictly used within methods of a class. Another way to refer to Self
is to use ::
.
References
- ^ Bound member functions - Using the GNU Compiler Collection (GCC)
- ^ ISO/IEC 14882:2003(E): Programming Languages - C++. ISO/IEC. 2003.
- ^ Stroustrup: C++ Style and Technique FAQ
External links
- The Design and Evolution of C++ by Bjarne Stroustrup - Addison-Wesley Pub Co; 1st edition (March 29, 1994); ISBN 0-201-54330-3
- More Effective C++: 35 New Ways to Improve Your Programs and Designs by Scott Meyers -- (1995) ISBN 0-201-63371-X
- Java this
- *this in C++
- Java This Keyword