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.
Insert the following code into Cat.jspp:
external $; module Animals { class Cat { string name; var $element = $( """ <div class="animal"> <i class="icofont icofont-animal-cat"></i> </div> """ ); void setName(string name) { this.name = name; } void render() { $("#content").append($element); $element.attr("title", name); } } }
We added a new field: ānameā with type āstringā. We also added a new method āsetNameā. It contains only one line of code:
this.name = name;
Youāll notice the method takes one parameter called ānameā:
void setName(string name) { this.name = name; }
In order to disambiguate the ānameā parameter from our ānameā class field, we need to use āthisā. When two conflicting names appear in the same method, this is known as āvariable shadowing.ā Itās a convenience so that we donāt have to come up with different variable names to describe the same concept. Thus, what our statement is saying is: set the ānameā class field (āthis.nameā) to the ānameā value passed in as an argument to our method.
The āthisā keyword inside a class refers to the class instance. Class instance methods, like the āsetNameā method we just defined, cannot be called unless the class has been instantiated first. Once weāve instantiated the āCatā class, we can call the āsetNameā method. When we call the āsetNameā instance method, the āthisā keyword refers to the class instance that executed the method. Thus, the above statement will set the ānameā field of the class instance that āsetNameā is being executed on; it is always unique to the class instance; thus, no two class instances will end up having the same ānameā field set after a āsetNameā method call.
Finally, we added the following statement to our ārenderā method:
$element.attr("title", name);
This will set the HTML ātitleā attribute of our cat HTML element that we render to the page. By setting the ātitleā attribute, we can set the text that appears when we hover over an HTML element. In this case, when we hover over our cat element, we will see its name.
Before we can see our results, we must set the names of our cats in main.jspp. Letās do that:
import Animals; Cat cat1 = new Cat(); cat1.setName("Kitty"); cat1.render(); Cat cat2 = new Cat(); cat2.setName("Kat"); cat2.render();
Make sure āsetNameā is called before the ārenderā method.
Compile the code. Once again, for Windows, Mac, and Linux users, the command is now all the same:
$ js++ src/ -o build/app.jspp.js
If the program compiled successfully, open index.html. Hover your name over each cat. You should see its name.