top of page

Object-Oriented Design

So, objects created using curly braces are called object literals. These can contain a number of properties including integers (numbers), string (text), booleans (true/false statements), and arrays [another collection of properties]. Within the object, all of the properties are just stored as data. You then need a draw function, with the object passed in as an argument and commands of what to do with each property in the object to tell the computer what you want to do with that object. Finally, you need to call the draw function with the particular object properties you want it to use.

But, what if you had multiple objects (e.g. danBaby, danToddler, danAdult, etc.) that used the same properties but had different values for those properties? You could just write each object out separately, but that is really slow and inefficient. Instead, we could create an overall object "Dan" that the baby, teen etc. versions are formal instances of. So we need to store an abstract data type called "Dan" in a variable with a constructor function (see below.) This constructor function contains (within parentheses) arguments which refer to the properties of the danTeen etc instances. This means that you need to create new instances of the abstract Dan object, where you pass in the particular information of that instance (in the object order in parens). This means that all of your objects have things that are shared about them and that are unique about each of them.

Object methods

So, we have made an abstract object type, "Dan", that we have initialized with the constructor "danTeen" and we could make more of "danBaby" etc. with different data for age etc. All of these are associated with properties (facts about the objects), but objects can also be used with functionality (things that they can do). To do this you need to make a prototype of the object that you then attach a function to. For example, we could attach a draw function to the object (see below.) If you do this, all instances of the object then have that function applied to them. When you create a function that can be called on an object it is referred to as a method.

Object inheritance

What if you have more than one abstract object type that are similar? For example, if I had an abstract type "Katie" that was nearly identical to the "Dan" I already have. Well, in that case you could create a "Human" object type that they both share, with all of the same arguments as the "Dan" object. In order to get the "Dan" and "Katie" prototypes to base their functionality off of the "Human" prototype, you need to use the code shown below. This is called the prototype chain. You need to put everything a prototype is based on above it in the programming. You can also put any extra functionality special to a particular object, e.g. "Dan" below it. For example, if I Katie likes to dance and I don't I could include that as a method below the "Katie" prototype only.

 
A table about creating more abstract objects.

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • LinkedIn Social Icon
  • Pinterest Social Icon
  • Facebook Basic Square

© 2023 by Coming Soon

Proudly created with Wix.com

bottom of page