Implementations C++ Java C# D Dylan Eiffel JavaScript Lua PowerShell Python Rust Self Xbase++
See also
References
Further reading
External links
{{lowercase}}
this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity of which the currently running code is a part. The entity referred to by these keywords thus depends on the execution context (such as which object is having its method called). Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, they can disambiguate variables and functions with the same name.
Object-oriented programming
In many object-oriented programming languages, this (also called self or Me) is a variable 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#, D, and PHP) generally use this. Smalltalk and others, such as Object Pascal, Perl, Python, Ruby, Rust, Objective-C, DataFlex and Swift, use self. Microsoft's Visual Basic uses Me.
The concept is similar in all languages: this is usually an immutable reference or pointer which refers to the current object; the current object often being the code that acts as 'parent' to the property, method, sub-routine or function that contains the this keyword. After an object is properly constructed, or instantiated, this is always a valid reference. Some languages require it explicitly; others use lexical scoping to use it implicitly to make symbols within their class visible. Or alternatively, the current object referred to by this may be an independent code object that has called the function or method containing the keyword this. Such a thing happens, for example, when a JavaScript event handler attached to an HTML tag in a web page calls a function containing the keyword this stored in the global space outside the document object; in that context, this will refer to the page element within the document object, not the enclosing window object.[1]
In some languages, for example C++ and Java, this or self is a keyword, and the variable automatically exists in instance methods. In others, for example Python, Rust, and Perl 5, the first parameter of an instance method is such a reference. It needs to be specified explicitly. In Python and Perl, the parameter need not necessarily be named this or self; it can be named freely by the programmer like any other parameter. However, by informal convention, the first parameter of an instance method in Perl or Python is named self. Rust requires the self object to be called &self or self, depending on whether the invoked function borrows the invocant, or moves it in, respectively.
Static methods in C++ or Java are not associated with instances but classes, and so cannot use this, because there is no object. In other languages, such as Python, Ruby, Smalltalk, Objective-C, or Swift, the method is associated with a class object that is passed as this, and they are called class methods.
Subtleties and difficulties
When lexical scoping is used to infer this, 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.
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.[2]
Open recursion
The dispatch semantics of this, namely that method calls on this are dynamically dispatched, is known as open recursion, and means that these methods can be overridden by derived classes or objects. By contrast, direct named recursion or anonymous recursion of a function uses closed recursion, with early binding. For example, in the following Perl code for the factorial, the token __SUB__ is a reference to the current function:
By contrast, in C++ (using an explicit this for clarity, though not necessary) the this binds to the object itself, but if the class method was declared "virtual" i.e. polymorphic in the base, it's resolved via dynamic dispatch (late binding) so that derived classes can override it.
This example is artificial, since this is direct recursion, so overriding the factorial method would override this function; more natural examples are when a method in a derived class calls the same method in a base class, or in cases of mutual recursion.[3][4]
The fragile base class problem has been blamed on open recursion, with the suggestion that invoking methods on this default to closed recursion (static dispatch, early binding) rather than open recursion (dynamic dispatch, late binding), only using open recursion when it is specifically requested; external calls (not using this) would be dynamically dispatched as usual.[5][6] The way this is solved in practice in the JDK is through a certain programmer discipline; this discipline has been formalized by C. Ruby and G. T. Leavens; it basically consists of the following rules:[7]
No code invokes public methods on this.
Code that can be reused internally (by invocation from other methods of the same class) is encapsulated in a protected or private method; if it needs to be exposed directly to the users as well, then a wrapper public method calls the internal method.
The previous recommendation can be relaxed for pure methods.
Implementations
C++
{{details|C++ classes}}
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.[8]
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.[9]
C++ lets objects destroy themselves with the source code statement: delete this.
Java
{{details|Java (programming language)}}
The keyword this is 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.[10]
C#
{{details|C Sharp (programming language)}}
The keyword 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.
One use of this in C# is to allow reference to an outer field variable within a method that contains a local variable that has the same name. In such a situation, for example, the statement var n = localAndFieldname; within the method will assign the type and value of the local variable localAndFieldname to n, whereas the statement var n = this.localAndFieldname; will assign the type and value of the outer field variable to n.[11]
D
In D this in a class, struct or union method refers to an immutable reference of the instance of the enclosing aggregate. Classes are reference types, structs and unions are value types. In the first version of D, the keyword this is used as a pointer to the instance of the object the method is bound to, while in D2 it has the character of an implicit ref function argument.
Dylan
In the programming language Dylan, which is an object-oriented language that supports multimethods and doesn't have a concept of this, sending a message to an object is still kept in the syntax. The two forms below work in the same way; the differences are just syntactic sugar.
and
Eiffel
Within a class text, the current type is the type obtained from the current class. Within features (routines, commands and queries) of a class, one may use the keyword Current to reference the current class and its features. The use of the keyword Current is optional as the keyword Current is implied by simply referring to the name of the current class feature openly. For example: One might have a feature `foo' in a class MY_CLASS and refer to it by:
[12]
Line #10 (above) has the implied reference to Current by the call to simple `foo'.
Line #10 (below) has the explicit reference to Current by the call to `Current.foo'.
Either approach is acceptable to the compiler, but the implied version (e.g. x := foo) is preferred as it is less verbose.
As with other languages, there are times when the use of the keyword Current is mandated, such as:
In the case of the code above, the call on line #11 to make_with_something is passing the current class by explicitly passing the keyword Current.
JavaScript
{{details|JavaScript}}
In JavaScript, which is a programming or scripting language used extensively in web browsers, this is an important keyword, although what it evaluates to depends on where it is used.
When used outside any function, in global space, this refers to the enclosing object, which in this case is the enclosing browser window, the window object.
When used in a function defined in the global space, what the keyword this refers to depends on how the function is called. When such a function is called directly (e.g. f(x)), this will refer back to the global space in which the function is defined, and in which other global functions and variables may exist as well (or in strict mode, it is undefined). If a global function containing this is called as part of the event handler of an element in the document object, however, this will refer to the calling HTML element.
When a method is called using the new keyword (e.g. var c = new Thing()) then within Thing this refers to the Thing object itself.
When a function is attached as a property of an object and called as a method of that object (e.g. obj.f(x)), this will refer to the object that the function is contained within.[13][14] It is even possible to manually specify this when calling a function, by using the .call() or .apply() methods of the function object.[15] For example, the method call obj.f(x) could also be written as obj.f.call(obj, x).
To work around the different meaning of this in nested functions such as DOM event handlers, it is a common idiom in JavaScript to save the this reference of the calling object in a variable (commonly called that or self), and then use the variable to refer to the calling object in nested functions.
For example:
Lua
{{details|Lua (programming language)}}
While Lua itself is not object-oriented, it contains a feature called metatables. When using metatables, one must first define what metamethod(s) they want to use. A metamethod is like an event trigger in that depending on what you do to a table (depending on the metamethods defined), something will be returned. In the example below, we replicated the behavior of objects by using the metamethod __index. Using this metamethod is a way of replicating the behavior of objects in that the table m is returned when indexing the regular table returns nil.[16]
For example:
PowerShell
In PowerShell the special automatic variable $_ contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.[17]
Also starting with PowerShell 5.0, which adds a formal syntax to define classes and other user-defined types[18], $this variable describes the current instance of the object.
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, and is conventionally called cls; these are primarily used for inheritable constructors,[19] where the use of the class as a parameter allows subclassing the constructor. In static methods (created with the staticmethod decorator), no special first argument exists.
Rust
In Rust, types are declared separately from the functions associated with them. Functions designed to be analogous to instance methods in more traditionally object-oriented languages must explicitly take self as their first parameter. These functions can then be called using instance.method() syntax sugar. For example:
This defines a type, Foo, which has four associated functions. The first, Foo::new(), is not an instance function and must be specified with the type prefix. The remaining three all take a self parameter in a variety of ways, and can be called on a Foo instance using the dot-notation syntax sugar, which is equivalent to calling the type-qualified function name with an explicit self first parameter.
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 ::.
See also
Anonymous recursion
Inheritance (object-oriented programming)
Self-reference
Schizophrenia (object-oriented programming)
References
1. ^Powell, Thomas A, and Schneider, Fritz, 2012. JavaScript: The Complete Reference, Third Edition. McGraw-Hill. Chapter 11, Event Handling, p 428. {{ISBN|978-0-07-174120-0}} 2. ^Using the GNU Compiler Collection (GCC) – [https://gcc.gnu.org/onlinedocs/gcc/Bound-member-functions.html Bound member functions] 3. ^"Closed and Open Recursion", Ralf Hinze, July 2007 4. ^Open Recursion, Lambda the Ultimate 5. ^"Selective Open Recursion: A Solution to the Fragile Base Class Problem", Jonathan Aldrich 6. ^"Selective Open Recursion: A Solution to the Fragile Base Class Problem", Lambda the Ultimate 7. ^Aldrich, Jonathan, and Kevin Donnelly. "Selective open recursion: Modular reasoning about components and inheritance." SAVCBS 2004 Specification and Verification of Component-Based Systems (2004): 26. citing for the JDK-adopted solution C. Ruby and G. T. Leavens. "Safely Creating Correct Subclasses without Seeing Superclass Code". In Object-Oriented Programming Systems, Languages, and Applications, October 2000. {{doi|10.1145/353171.353186}} also available as technical report TR #00-05d 8. ^{{cite book| year = 2003| title = ISO/IEC 14882:2003(E): Programming Languages - C++| publisher = ISO/IEC}} 9. ^Stroustrup: C++ Style and Technique FAQ 10. ^Barnes, D. and Kölling, M. Objects First with Java. "...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."{{citation needed|date=February 2013}} 11. ^De Smet, Bart, 2011. C# 4.0 Unleashed. Sams Publishing, Indianapolis, USA. Chapter 4, Language Essentials, p 210. {{ISBN|978-0-672-33079-7}} 12. ^NOTE: The line numbers are for reference purposes only. Eiffel does not have line numbers in the class text. However, there is a line number option in the Eiffel Studio IDE, which can be optionally turned on for reference purposes (e.g. pair programming, etc). 13. ^Crockford, Douglas, 2008. JavaScript: The Good Parts. O'Reilly Media Inc. and Yahoo! Inc. Chapter 4, Functions, p 28. {{ISBN|978-0-596-51774-8}} 14. ^Powell, Thomas A, and Schneider, Fritz, 2012. JavaScript: The Complete Reference, Third Edition. McGraw-Hill. Chapter 5, Functions, pp 170–1. {{ISBN|978-0-07-174120-0}} 15. ^Goodman, Danny, with Morrison, Michael, 2004. JavaScript Bible, 5th Edition. Wiley Publishing, Inc., Indianapolis, USA. Chapter 33, Functions and Custom Objects, p 987. {{ISBN|0-7645-5743-2}} 16. ^https://www.lua.org/pil/13.4.1.html 17. ^{{Cite web|url=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables|title=PowerShell: About Automatic Variables|last=msdn|first=|date=|website=docs.microsoft.com|language=en-us|archive-url=|archive-date=|dead-url=|access-date=2018-03-22}} 18. ^{{Cite web|url=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes|title=about_Classes|last=msdn|first=|date=|website=docs.microsoft.com|language=en-us|archive-url=|archive-date=|dead-url=|access-date=2018-12-17}} 19. ^Unifying types and classes in Python 2.2, Guido van Rossum, "[https://www.python.org/download/releases/2.2.3/descrintro/#__new__ Overriding the __new__ method]"
Further reading
Meyers, Scott, 1995. More Effective C++: 35 New Ways to Improve Your Programs and Designs. {{ISBN|0-201-63371-X}} Scott Meyers
Stroustrup, Bjarne, 1994. The Design and Evolution of C++. Addison-Wesley Pub. Co. {{ISBN|0-201-54330-3}} Bjarne Stroustrup