- Part 1: Creating and Using Variables
- Part 2: Data Types and Arrays
- Part 3: Functions
- Part 4: Objects
- Part 5: Document Object Model
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.
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.
All objects have something called this. In the scope of an object, the “this” keyword refers to itself. It can reference variables and other properties that it has through the reference to its this.
Similar to how a property was referenced using a period between the object and its property, a property can use the values of it sister properties through using 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.
Creating New Objects
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.
The example is a function, and values can be passed as parameters. Because functions are also objects, the use of 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.