Javascript 101 - Embedding objects & Arrays — 03
Before reading this, you can look at Javascript 101 — Arithmetic Operators, Code Editors, Functions, Objects, and Arrays — 02.
Objects inside them can have objects, we can call them embedded objects. Actually, in real life, it is like that. Let’s think of a plane, planes have engines, wings, control units, tires, etc. These are all objects that create another object. When they are together they create a plane but without wings engine, control unit, and tires create a car also. So we can think of embedded objects like that.
Now let's turn to our example,
Here as we see, we have added, engine as an object, and inside car object pistons as an array. Also, we have created tires as an array inside in the car object, and in that tires object, we have defined 4 objects giving names of all of these maker names.
After writing our code in Brackets IDE in example.js we can run it via console. Here it is the results of the car object in the console.
Car as an object, tires as an array, and inside of tires objects.
So now, how can we access the members of objects? As in all languages, we can access the members of objects with `.` , here it is the examples.
- the car has a property named color and it is blue;
- the car has an object named engine and it is size 1.4;
- the car has an object named as an engine and has an array named pistons which the first element of it (0) is named pistons.
- car is an object and tires in it is an array that has 4 objects which the first object of it is `continental`.
Now let's look window object;
window object have lots of properties as we can see some of them is our array.
How can we reach these properties is also easy. With dots,
Here in the window object, we see the alert function. In console without writing `()` it is said to us this is a function. But when we write it window.alert() and run it in our web browser pop up an alert.
Actually, it is what we use as PoC during penetration tests for searching XSS vulnerabilities.
Another method for reaching the elements of objects and arrays is using `[]`. Let's examine the example below together.
When we try to use `[]` and `.` together console gives us an error. So we can use only one of them.
Javascript makes all processes in `[]` and gives us results of it. As it is shown in the car object, we can reach the engine size using two times `[]`.
Also, we can define a pointer and with the help of that pointer we can use `[]`. Also defining we can do some arithmetic calculation.
Here are some examples of Object Modifications.
Here are examples of array modifications.
bigaarray.shift(); /*Deletes the first element from the array*/
bigaarray.unshift(“Add new elements from the beginning”, function(){}, {}); /* we can add new elements with unshift method as we see here we have added string, function, and an object */
bigaarray.splice( 2,1, “here”, “from”, “that”, “point”, “I’m adding new elements”); /* .splice( start point, count , new elements) in our example, from 2 where we starting we have removed one element and add five new elements */