Jump to content

this (computer programming)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Omnipaedista (talk | contribs) at 17:38, 8 May 2013 (the article already has a lead section; you don't provide citations; as it says in WP:SYNTH, one must be careful to refrain from combining material from multiple sources to reach or imply a conclusion not explicitly stated by any of the sources). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, keywords are used in source code to denote certain actions or logical procedures that the programmer, or developer, wishes the computer to perform when the program is compiled and run; this is such a keyword. In most computer languages this is used by a body of computer code that lies within a larger grouping of code in order to refer to this larger grouping of which it is a part. When code wishes to call other code, the keyword this is often used to refer to this larger grouping of code, allowing smaller code elements within it to refer to one another, often in a hierarchical way.

In some languages, a similar keyword is signified by self or My. The precise meaning of the keyword is often dependent upon the context, and this may refer to different bodies of code depending upon the context in which it is used. In addition, different computer languages use the keyword in subtly different ways.

Object-oriented 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#, D, and PHP) generally use this. Smalltalk and others, such as Object Pascal, Python, Ruby, and Objective-C, 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 Python and Perl 5 which require explicit use of this, the first parameter of an instance method is such a reference. It needs explicitly to be specified. In that case, 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 and Python is named self.

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 they are called class methods.

Subtleties and difficulties

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, but changes the identity of the object that the rest of the code in the method refers to, which can result in undefined behavior.[citation needed]

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:

use feature ":5.16";
sub {
  my $x = shift;
  $x == 0 ? 1 : $x * __SUB__->( $x - 1 );
}

By contrast, in C++ (using an explicit this for clarity) the this binds to the object itself, but the method is resolved via dynamic dispatch (late binding):

unsigned int factorial(unsigned int n)
  if (n == 0)
    return 1;
  else
    return n * this->factorial(n - 1);

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]

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.[7]

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.[8]

C++ lets objects destroy themselves with the source code statement: delete this.

Java

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.[9]

C#

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.[10]

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 hasn't 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.

object.method(param1, param2)

and

method (object, param1, param2)

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 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.[11][12] It is even possible to manually specify this when calling a function, by using the .call() or .apply() methods of the function object.[13] For example, the method call obj.f(x) could also be written as obj.f.call(obj, x)

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 ::.

See also

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) – 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. ^ ISO/IEC 14882:2003(E): Programming Languages - C++. ISO/IEC. 2003.
  8. ^ Stroustrup: C++ Style and Technique FAQ
  9. ^ Barnes, D. and 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]
  10. ^ De Smet, Bart, 2011. C# 4.0 Unleashed. Sams Publishing, Indianapolis, USA. Chapter 4, Language Essentials, p 210. ISBN 978-0-672-33079-7
  11. ^ 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
  12. ^ 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
  13. ^ 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

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