Objects
Objects
JavaScript is a multi-paradigm language. This means that, among others, it supports a way of thinking about programming called object-oriented. In this paradigm, objects are at the center of how problems are solved using the language.
In order to represent a complex problem or system, it is broken into its objects and these are described by using properties. Similar to how an animal might have a name and height, for example, these would be properties of an animal object.
Object Literals
The easiest way to think about an object in JavaScript is something which is written literally. Known as an object literal, an object can be created by using a variable and giving it properties. Similar to an array and its indexes, the properties can be accessed through the name of the variable.
Unlike arrays that used square brackets and a number, using the object, a period, and then the name of the property, its value can be used.
Objects Can Reference Themselves
We can refer to properties from inside an object by using the same dot notation we used externally, but with the keyword this.
Functions, as another type of value, can also be used within objects. Using them with the keyword “this”, in fact, allows them to use other values and return data based on tasks or calculations
You use this when you have a function inside an object that's something that is inherent to the object. So if zombies want brains:
Every instance of zombie would then want brains.
Creating New Objects
Functions are objects. More correctly: functions can be treated as an object. They are not separate things in JavaScript.
In the previous example of using a variable named animal, its properties could be referenced and functions could be added to it. However, to create a new animal, all of its code would have needed to be copied and pasted into a new variable to make a new animal. That is not very efficient when dealing with a large number of entities.
In JavaScript, then, it is possible to create what is known more generally as a class in the object-oriented programming paradigm. It is a generic model where data can be added and new versions made without needing to manually set the data or copy and paste code each time.
To create a new animal based on the class animal, the use of the keyword “new” is needed. This tells JavaScript to take the code and make a new copy of it for this variable.
The example is a function, and values can be passed to the function as parameters. Because functions are also objects, the “this” keyword can be used to save the values passed to it. As shown in an earlier example, functions can also be both inside other functions and as a property of objects.
Starting with a function as the basis, new objects can be created based on the model of an original, generic object. This is a core concept of the object-oriented programming paradigm, and something JavaScript easily supports for using functions as dynamic objects.
Why are objects important?
When thinking in the object-oriented programming model, objects are at the core. One way to think about objects is to define a "blueprint" for later usage. That is, for the object representing different animals, it had a name and a type. Each new object of type animal used their own name and type.
In a more complex project, objects could be things like individual windows and the content within them. Each window would be based on one model, window, and have its own values and content per window.
Play with the Code (Repl.it):
Take a moment to play with the code and concepts in the embedded Repl.it viewer below. You might want to try changing the greeting or adding new animals.