[Ethereum] the ‘this’ keyword in Solidity

solidity

What is the specification of the 'this' keyword in Solidity?
How does it work?

Sample code from Solidity Features ยท ethereum/wiki Wiki

contract Helper {
  function getBalance() returns (uint bal) {
    return this.balance; // balance is "inherited" from the address type
  }
}

Best Answer

According to this guide:

Contracts Inherit all Members from Address.

It means that this is the pointer to the current instance of the type derived from Address (in your case - current instance of Helper), and balance is a member of Address.

It helps you distinguish between contract's own balance and any other balance, like in this example:

address payable x = 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF;
if (address(x).balance < address(this).balance) 
    address(x).transfer(10);

And here is a some kind of definition (from this doc):

this: the current contract, explicitly convertible to address

Related Topic