Class Fields

Beyond methods that define the behavior for a class, classes can also hold data of their own. At its core, you can think of classes as data plus methods that operate on that data. For example, with pets like cats, we may want to be able to give the cat a name. The name is the data and examples of methods might be functions that allow us to set or change the name.

Class fields allow us to specify data that is exclusive to the class. Fields are just variable declarations:

class Cat
{
    string name;
}

Class fields differ from regular variable declarations because they can only be accessed via the class or class instances. Furthermore, we declared a ‘name’ field that is exclusive to a class instance above; this means that each unique instance of the class will have its own unique name data.

Data that is unique to a specific instance can be very useful, especially when dealing with a large number of similar objects. However, our current code that renders the cat to the page actually doesn’t keep data unique to an instance! Let’s observe; change the main.jspp code as follows to call render() a second time:

import Animals;

Cat cat = new Cat();
cat.render();
cat.render();

Compile the code and open index.html. You should see two cats rendered, but we have only one instance of the ‘Cat’ class. This is not ideal; rather, we should like to require two ‘Cat’ instances if we want to render two cats to the page. In order to do that, we have to examine our render() code:

class Cat
{
    void render() {
        var $element = $(
            """
            <div class="animal">
                <i class="icofont icofont-animal-cat"></i>
            </div>
            """
        );

        $("#content").append($element);
    }
}

Do you see what’s wrong?

Each time the render() method is called, we are calling jQuery to create a new object, and then we render that new object to the page via jQuery’s append() method. What we need to do is to make the creation of the page elements exclusive to class instances. We can do this by simply moving the variable declaration in render() to a class field. Here’s our full code:

external $;

module Animals
{
    class Cat
    {
        var $element = $(
            """
            <div class="animal">
                <i class="icofont icofont-animal-cat"></i>
            </div>
            """
        );

        void render() {
            $("#content").append($element);
        }
    }
}

We’ve done nothing in the above code other than move the variable declaration from render() to the class itself so it becomes a field. Now, the field will only be initialized when we instantiate the class. In other words, the jQuery function call you see that turns HTML into a jQuery object will not be executed until we instantiate our class using the ‘new’ keyword.

Keep your code exactly as you had it with the two render() calls for the same class instance. However, with the updated Cat.jspp, compile your code again. Open index.html. You should see only one cat rendered to the page.

Now let’s try creating two cats by updating main.jspp to use two class instances:

import Animals;

Cat cat1 = new Cat();
cat1.render();
Cat cat2 = new Cat();
cat2.render();

You should now see two cats, one for each class instance:

JS++ | Fields and Methods

Similar Reads

Creating and Rendering an Animal

Open ‘src/Animals/Cat.jspp’ and enter the following code:...

Compiling

Compilation is going to be slightly different compared to our previous tutorial examples due to our deep directory structure. Mac/Linux users won’t have to make much of an adjustment, but, for Windows users, we have to start using the Command Prompt....

Styling the Animals

Right now, our cat is quite small and hard to see. Let’s modify the styling to make our cat bigger and clearer....

Class Fields

Beyond methods that define the behavior for a class, classes can also hold data of their own. At its core, you can think of classes as data plus methods that operate on that data. For example, with pets like cats, we may want to be able to give the cat a name. The name is the data and examples of methods might be functions that allow us to set or change the name....

Naming our Animals with Fields and Methods

While our first example for class fields involved naming, we never actually added a name field to our classes. However, rather than just naming our cats, let’s also make it tangible by making the name visible in the web browser when we hover over the animal. As we showed in our previous example, fields like the name should be unique to the class instance – no two cat elements on the page should be showing the same name if they were given different names....