Java: This keyword

This entry is part 25 of 54 in the series Learn Java

Inside the constructor, as well as in other methods belonging to the object, a special keyword called this can be used.

This keyword is a reference to the current instance of the class.

If, for example, the constructor’s parameters have the same names as the corresponding fields, then the fields could still be accessed by using the this keyword, even though they are shadowed by the parameters.

package javaapplication19;

public class JavaApplication19 {

    static class MyRectangle {
        
        int x, y;
        
        public MyRectangle(int x, int y){
            this.x = x;
            this.y = y;
        }
        
        public int getArea() {
            return x * y;
        }
    }

    public static void main(String[] args) {
        MyRectangle r = new MyRectangle(10, 20);      

        int area = r.getArea();
        System.out.println(area);      
    }
}

this

 

PHP & OOP: $this variable

This entry is part 5 of 11 in the series PHP & OOP

In this example, the $this variable is a reference to the current object, which is itself, SimpleClass.  The this reference is commonly found in other object-oriented programming languages.

<?php
class SimpleClass
{
    public $greeting = 'Hi There';
 
    public function displayGreeting() {
        echo $this->greeting;
    }
}
 
//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayGreeting();
 
?>

The $this->greeting is to access its own variable $greeting.

If you access the $greeting variable without the $this reference, the browser will throw an undefined variable greeting error message.

You can try the below code to see the error message.

<?php
class SimpleClass
{
    public $greeting = 'Hi There';
 
    public function displayGreeting() {
        echo $greeting;
    }
}
 
//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayGreeting();
 
?>

this_error

jQuery: Events

This entry is part 7 of 33 in the series jQuery Tutorial

After selector, Event is the second most important component of jQuery.
We use a click event to illustrate jQuery event.

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <script>
        $(function () {
            $('p').click( function () {
                        $('p').css('background-color','yellow');
                    }
            )
        });
    </script>
</head>
<body>
<p>If you click me I will change</p>
</body>
</html>

Try The Code

Notice how we use the click event to act on <p> element.  We want the font color to change and so we put a function inside the click event.

Similar to .click, we use .css to change the background color of the font.

In jQuery, there is a this object that we use to refer to the current element that it is acting on.  In this example, the current element is <p>.

We can replace the p element with this object as below.

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <script>
        $(function () {
            $('p').click( function () {
                       $(this).css('background-color','yellow');
                    }
            )
        });
    </script>
</head>
<body>
<p>If you click me I will change</p>
</body>
</html>
Try The Code