Javascript Tutorial | Embedding Objects & Arrays | Ep13

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
objects can contain other objects also arrays are objects you can think of it like a car engine or pretty much any object even your computer is made up of many many objects when we look at these objects we see one entire object we see the overall hierarchy but you can dig down deeper and deeper just like a mechanic takes a car engine to pieces you have the car object you have the engine and the engine has many objects inside of it including the head gasket Pistons and all the rest of the rods looks and crannies that go in there and steering wheel the interior the seats the headrests all the rest of it is all based on objects and when we group these objects together in a giant hierarchy we have one object but we understand that objects contain objects and that's how it works in programming as well so here we're going to start out with the my RS file and I'm first of all going to create a symbol a memory pointer and we're going to create a car memory pointer and we're going to assign the object whenever we reference car this will pull up this object in memory and then we want the make of the car for example will say Volvo and then we have the speed 160 miles an hour so that's very quick but then also what I'd like to do is I'd like to put an engine into my car so let's go ahead and create an engine object now again I'm using the coulomb for assignment and then all of those are put in another object and this object is still going to help describe this object if I know an object contains an engine for example I know that it's going to drive the car it's going to have some kind of movement so this is actually still called a property this key and value pair as we have key and value pairs right here this is still called a property so even if you have an object which is a standard object or an array object it doesn't matter it is still classed as a property so we are creating a property here the engine property and inside of here we can have let's say the size of the engine which is going to be a 2-liter there's a floating-point number for you and then also we can say make and the mate can actually be from another manufacturer such as BMW and so forth and also we can have the fuel type as well the fuel type could let's say be petrol so there we go and we can keep going with this for example I could have Pistons as well and again they can be an array for example and we can have an array of piston one like so and piston 2 now don't forget arrays or objects we must remember this so I could make this another standard object or I am instead making this an array because we have an array of Pistons we have many Pistons and typically every piston is the same so what I could do is as an array is a type of object and forget it is an object type I can actually embed objects into the array so for example we can have an object for each piston now a car doesn't typically come with two Pistons but this one does so we have the Pistons we have an array of Pistons and each piston is an object like so so objects can contain objects arrays contain objects and objects can contain arrays they're just classed as objects really to the programming language so now we have a list of all the Pistons because they all carry the same similar type of thing and so each piston can let's say just have the maker we can add the maker and we can set that to BMW and you can go into even more detail by embedding more objects into this object and arrays and so forth you can just keep on going embedding objects inside of objects and you can see there is a hierarchy a hierarchy means a structure just like the food chain is a hierarchy or your place of work has a hierarchy with the boss sitting at the top and then it goes down to directors and other things like that so that is the hierarchy we have a structure in which our object is created so we have the main car object with these properties then we have the N Jim that's embedded inside of it this is another property but it's an object embedded inside of this so there we go we've embedded that then we have the size of the engine to make the fuel and so forth and then finally we have another object which is the array and the array has multiple piston objects inside of it so that really is the whole hierarchy for our car object now if I go ahead and save this and I open it up inside of the browser let's go ahead and type in car there is my car object so we referenced it in memory and you can see in the make in the speed but then you can also see engine is an object and I can expand that and you'll be able to see we have the fuel and make and then also we have pistons now Pistons is an array and it tells us how many elements are contained within the array this is really nice you can see here we have two elements two objects there's the first and then there's the second and don't forget to separate out each value with the comma and there it is I have an array and I have two objects so we have the first object with the index of zero don't forget is zero index so this is the zero index object and again this could be a string or it could be a number it could be anything you really wanted it to be and there we go so we have an object maker BMW and then we also have the second piston object which is exactly the same let's change that to BMW two in fact so we can see a bit of a difference here so car open this up go to the engine go to the Pistons and the second object with the index of one is maker BMW 2 and now the first one is make a BMW so now we have an entire hierarchy a structure and a stack and also I can create an array and again I'm going to create a memory pointer to my array and an array can contain a variety of things such as a string or a number or in fact it could contain another array so I can have another array and I can say and I can say 200 I can also come out of that so there's the string there's the number there is the embedded array an array of side of an array we have embed 200 and if I stack this down a little bit like so you'll be able to see each value on a new line and I can also embed an object inside of here as well so I can say car is going to be Ford there is an object and again I can embed objects inside of objects arrays can embed objects and objects can embed arrays these are all properties by the way if it's not a function as the value its classes of property and here's something really cool is the fact that we have methods in objects so I can create a function or a method called drive and it returns the string drive so that is a method within our car object directly you could put this inside of the engine object and even what you could do is take this function right here and you can place it inside of an array so you can place functions inside of an array now what's important to note here is you don't have a name these are called methods because methods have a key name that we can define in a standard object and then we have the subroutine that will perform that particular action in an array we don't have that anymore we cannot define a name for our function in the same way it's given a number so let's take a look at this now let's take a look at the array so if I go ahead and refresh in car you can now see that we have drive and by the parentheses we can see that it is in fact a method based subroutine waiting to be executed and then also you have array now with array we have 0 the first element is the string 100 is the second element with the index of 1 2 is an array with two elements which is embed and 200 and you can see that there has two elements zero embed 1 200 then also you have the object as well the object that I've embedded has one property called car and the value Ford and there it is then finally we also have the fourth element now you'll notice the fourth one is in fact a function and there is my function this is the drive function but I can't give it a distinct name the memory pointer is actually the number 4 this time not as in the standard object which the memory pointer was drive so that's it we've established that objects can contain other objects object can contain the array type object and the array type object can contain objects and other array objects as well you can just keep embedding objects and arrays inside one another vice-versa doesn't matter what order you do it in you can keep on embedding and embedding so go ahead and take a look at playing and experimenting and try to do this yourself try to make this hierarchy yourself write it from scratch and really learn how the syntax flows and how things can be embedded inside of other things try to do it for your computer or for let's say other components or other objects that you work with in your house try to make an object hierarchy whether it be a cabinet with drawers and so forth let's try to really learn this hierarchy and the syntax that goes behind it
Info
Channel: Programming With Avelx
Views: 3,379
Rating: undefined out of 5
Keywords: objects, arrays, embedding objects inside of objects, arrays inside of objects, objects inside of arrays, hierarchy of objects
Id: WOBlhuGHbdE
Channel Id: undefined
Length: 10min 35sec (635 seconds)
Published: Sat May 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.