Creating and Rendering an Animal

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

external $;

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

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

One thing you may immediately notice is the multiline string (“””…”””). When we surround our string in triple quotation marks, we can write a “multi-line” string. Multi-line strings are a feature of JS++ that are especially useful when dealing with HTML. You’ll notice that, with multi-line strings, we didn’t have to escape our double quote characters (“) or newline characters.

Besides the multi-line string, we’re just passing some HTML to jQuery via the $(…) as usual. jQuery will recognize this as HTML and dynamically create the HTML elements for us. However, the dynamically-created HTML elements are still only held in memory unless we render it to the page. We do that with this line:

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

You’ll notice we named our variable for the jQuery element as $element. It is a common naming convention to prefix variables holding jQuery objects with the $ sign.

You may also have noticed that we declare the data type for the jQuery $element variable as ‘var’. As we discussed in previous chapters, there may not always be a corresponding JS++ data type for complex objects so we can just use ‘var’ in these cases (or even just for convenience).

Side Note: jQuery and the Document Object Model (DOM) API for manipulating web pages can be classified as “highly-dynamic.” Some large companies were incorrect to try to add static data types in these cases as there are many corner cases where static data types will be incorrect or fail – such as for ECMAScript “host objects, ” which includes the DOM API, where “implementation-defined” behavior, such as different browser implementations of the same DOM API methods, is valid according to the language specification (and indeed occurs in real-world implementations). Another example is the garbage collector implementation in versions of Internet Explorer where the GC can reclaim non-cyclic, in-use host objects (such as the “htmlfile” ActiveXObject used for real-time streaming) too early, a scenario that is not amenable to static analysis. Despite their convenience, the static data types in these cases create a “false sense of security.” However, discussion of these topics is outside the scope of this tutorial.

You will notice that we declared a function inside our ‘Cat’ class. A function declared inside a class is commonly called a “class method” or just “method” for short. Functions that are not members of a class are known as “free functions.”

We declared a class inside a module, so this file by itself will not compile. We need to create a “main file” to act as the program entry point. In your ‘src’ folder (the parent folder of the ‘Animals’ folder), you’ll recall we created an empty main.jspp file. Open the main.jspp file and enter these lines of code:

import Animals;

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

We first instantiate the class to get a class instance. Once we have a class instance, we call the ‘render’ method on that specific 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....