this Summary In JS++, 'this' refers to the current instance of a class. The 'this' keyword can also be used to invoke a class constructor or access instance members of a class. For JavaScript code, the 'this' keyword has varying semantics depending on the context in which it is used. Syntax this([ argument1[, argument2[, [...] argumentN ] ] ]) this[.member] Parameters argumentN Any legal expression to supply as an argument to the constructor call. member A member of the class whose instance the 'this' keyword refers to. Description The this keyword refers to the current instance of a class in JS++, and its use varies depending on the context in JavaScript. 'this' in JS++ For JS++, this refers to the current instance of a class, but it can also be used to invoke a constructor. Calling Constructors In order to invoke a non-static constructor of the current class, use the this keyword as a function call. 12345678class Dog{ public Dog() { this("Fido"); // Invoke constructor that asks for a dog name } public Dog(string name) { }} For overloaded constructors, the compiler will attempt to match the argument types with the constructor parameter types. If there are no matching constructors, a compile-time error will be raised. If there is more than one possible match, the compiler will use the most specific overload first before attempting to convert argument types. See the function overloading page for more information on overloading is handled. Calling constructors with the this keyword implies calling an overloaded constructor since recursive constructor calls are not allowed. Accessing Class Members Members of the current class instance can be accessed using the this keyword: 123456789101112131415// Access instance field of a class using 'this' import System; class Dog{ private int legs = 4; public int getLegs() { return this.legs; // Returns 'legs' field }} Dog dog = new Dog();Console.log(dog.getLegs()); // 4 12345678910111213141516// Accessing instance methods of a class using 'this' import System; class Dog{ public Dog() { this.bark(); // Calls the 'bark' method } public void bark() { Console.log("Woof!"); }} Dog dog = new Dog(); // "Woof!" Variable Shadowing Variable shadowing occurs when a variable name declared in a scope is the same as the name of a variable in an outer scope. Variable shadowing can occur when a method takes a parameter with the same name as the class field it's intended to modify. In order to disambiguate a class field from a parameter of the same name, the this keyword can be used to refer to the class field: 12345678910111213class Dog{ private string name; public void setName(string name) { // 'this.name' refers to 'name' field of 'Dog' class // 'name' refers to the parameter for the constructor this.name = name; }} Dog dog = new Dog();dog.setName("Fido"); 'this' Casting inside ClassesPartially Implemented When this is used inside a class method or closure with an internal return type such as int or bool, this refers to the current class instance. The rules differ slightly when the method or closure has a function return type. By default, even when the return type is function, this still refers to the class instance. 12345678910111213import System; class Foo{ int bar = 1; function getBar() { return this.bar; // 'this' refers to 'Foo' instances here and can access the 'bar' field }} Foo foo = new Foo();Console.log(foo.getBar()); // 1 However, sometimes, it is desirable for this to have an external type and retain JavaScript this semantics (e.g. for jQuery). In such cases, JS++ allows this to be cast to external and used exactly as in JavaScript: 123456789101112131415external $; // jQuery class Foo{ function clickHandler() { var $this = $((external) this); $this.text("Click"); } void mouseoverHandler() { // Casting works inside 'void' and return types other than 'function' too var $this = $((external) this); $this.text("Hover"); }} For programming styles that prefer explicitness, the this value can also be cast to the current class. To avoid excessive casting, it can be desirable to capture the relevant this values once and re-use later: 12Foo _this = (Foo) this; // internalvar $this = $((external) this); // external Fluent Interfaces The this keyword can be used to return the class instance in order to develop a fluent interface via method chaining: 12345678910111213141516171819202122import System; class NumberPrinter{ public NumberPrinter one() { Console.log(1); return this; } public NumberPrinter two() { Console.log(2); return this; } public NumberPrinter three() { Console.log(3); return this; }} NumberPrinter printer = new NumberPrinter();printer.one().two().three(); 'this' in JavaScript The value of the this keyword in JavaScript depends on how and where it is called. The following table lists how the this keyword can change for JavaScript code: Description Example 'this' Value Method Call someObject.someMethod(); someObject Free Function Call foo(); global (undefined in ES5+ strict mode) Instantiating Constructor Function new Foo(); The instantiated object eval eval("this.foo"); Value of this where eval was called Using Function.prototype.apply foo.apply(thisObj, [ arg1, arg2, argN ]); thisObj Using Function.prototype.call foo.call(thisObj, arg1, arg2, argN); thisObj See Also super new class External Links Understanding JavaScript's this keyword Share HTML | BBCode | Direct Link