I chose this topic because I think it is an interesting way of programming and one which I do not fully understand the uses of.
Object oriented programming involves defining a set of objects and giving each one attributes and behaviours.
In Javascript, we use classes to define objects and can assign multiple attributes to the object before defining different cases of that object.
Objects can also have methods which is almost like a function specific to that object and which must be called alongside an object.
Objects are very useful for defining methods for each entity in a game or for other uses
Getter and setter messages can be used to return or set the value of an attribute, an example of this will be found further down the page
For example, you may have a class, animals, with 2 characteristics, color and name.
You can also create subclasses which inherit data from the parent class
For example, there could be 2 kinds of animal, cat and bird, which each have subclasses, lion and tiger and penguin,duck and swan.
Cat and bird are subclasses of animal but superclasses of lion,tiger, penguin, duck and swan.
Each of these subclasses would share the color and name attributes but cat may have the extra attribute run and bird may have the extra attribute fly.
This could be used if there was an animation where the cats needed to run and the birds to fly. In this case, maybe run and fly would actually be methods rather than attributes.
An example of this can be seen below (the triangle is a bird and the circle a cat because I can't draw animals), with the addition of x and y values in the animal class to allow for movement:
This simple one only uses the subclasses bird and cat but, as mentioned before, you can add many more subclasses to each subclass
The key word to define an object is class Name and to create a subclass you use class Next extends Name, the code used to define the animal class is below:
An example of the Cat class can be found here:
These classes mean that each object can have associated methods unique to that object but defined for all instances of that method, for some reason though the cat doesn't seem to show.
Classes can also be used for other things, such as getter and setter, below is an example where you can change the colour of the shape by clicking the colour circles and change the shape by clicking the other shapes, example code will follow:
The code for this is actually quite simple, much more so than if you were using functions and states!
Using objects with their own functions means that I could create another shape and make it so that one could only be one colour or was always a rectangle by simply not 'setting' the shape or colour values of that shape when the button is pressed
This use of objects is much easier as I haven't tried to use both functions and methods
The change() method simply checks what the shape and colour fields are set to, and is only called when a button is pressed, meaning the shape only updates when it actually changes
By using the method of an object attatched to a button, the website is able to become properly interactive, this is very useful as whilst the example above is very simple, it could be used to create a canvas on which multiple shapes could be added or dragged by the user. The only catch is that the method would have to be called for all instances of that object