top of page

Finally, you know it's great to practice looping but we should try to refactor code whatever we can. So those of you who are a keen idea there you may have realized an opportunity here that meant we didn't have to create another function or another loop. We can take care of all of our logic in our initial loop so let's rewrite our function to do that. So instead of Konst go for the best digital marketing companies in India, all members will do Konst. Let's see new fam group equals new family group like so then when we are iterating over here instead of pushing we can just check here if relationship equals father or relationship equals mother then we can do new fam group Parents push new member which we're defining here we're instantiating here.

 

Push into the parents. Else new fam group that children push new member K and then we don't have to invoke another function at all. We just need to after the loop is complete return the new fam group like digital agency so this should give us the exact same results and we were able to save ourselves a lot of trouble. We don't have to worry about looping again after another loop. So if I get rid of this code and we should be able to see the same result here that be clear. Run it again.

 

And looks like it was successful. And just to be sure we'll check in our dev tools. We have our children and our parents and it looks accurate to me. Cool. So you know I love coming back and writing code refactoring code it just kind of makes you really evaluate. Did I really need to create a separate function here or I'm already looping all over all of these objects and I already have a relationship field so that's something best digital marketing companies in India I can easily check in my initial loop and then assign it after creating a new family group. Right so combining like loops and instantiating objects is definitely confusing at first but once you get the hang of it it does start to make logical sense. It makes sense when we invoke create a family.

 

OK, we'll create a family means I should probably instantiate a family group and then I'm going to instantiate all the members which I will afterward push into the group. Right now you have a nice little concise function that takes care of that for us and if we had another family we could easily reduce this rate so we can say Konst another family and we can have one Wilson Billy-Bob father and we have two Wilson Joe Joe Joe laserjet son for example.  like best digital marketing companies in India It's a father-son family and all we needed to do is Konst Wilson's sequel's create family passing the new family object which is another family and now we have a nice reusable function that we can use to instantiate as many objects as we need it and just to do a sanity check consul. The Wilsons clear run OK look successful we have a child named Joey Joe Joe and a parent named Billy Bob Wilson. Good stuff.


That about covers the basics of classes and object-oriented programming in terms of javascript. Now I will mention this is a difficult concept to grasp especially if you're a beginner. And I also would like to say you know in a lot of your early projects and a lot of your early code probably won't come across too many cases where you will need to instantiate objects you know things like inheritance instantiating objects. This can largely be avoided for beginners. You will eventually come to a point though where you'll be happy that you do understand how inheritance works in terms of Javascript. So imagine that you're making some kind of personal site and you are creating some kind of shopping cart system.

 

It makes a lot of sense to instantiate objects in a type of shopping cart. For example, maybe users can select some kind of T-shirt and they can select how many sizes or what size they want. How Many Of a certainty should they want a price? And you can basically add event listeners So OK so this user is increasing the amount and they're going to click. Add this to cart so as soon as you detect that OK best digital marketing companies in India I've detected someone clicked Add to Cart. So I'm going to instantiate a bunch of objects to represent each of these T-shirts and each object can have different properties. Right. Not Jepp object can have an ID an object can have a color size what type of shirt it is. And that's just one simple example of a shopping cart sorting out when you're just learning. You know it's unlikely you'll need to do that but hey it couldn't hurt to kind of familiarize yourself with that.

 

And in the context of inheritance, you know maybe you have a shirt which inherits from clothing and all clothing have certain things in the common right. All clothing will have some type of fabric and a size and a style for example. And then you can just kind of filter down to maybe teeshirt inherits from clothing. What's different about a T-shirt. Well, the style is different so maybe you could move the style down into the best digital marketing companies in India the T-shirt class that way in your shopping cart if someone comes along and adds. I'm going to look at these jeans and add them to my shopping cart. While jeans as well, of course, they have a fabric material. They have a size and jeans will have their own style as well.

 

So these are the kind of situations where it makes sense to instantiate objects things like employees or family members where you know it doesn't make sense just to have a generic object but it would make sense to have individual objects that are instantiated with different properties. So think about it write some code maybe see if you can't implement a simple shopping cart type code using Jaspan in your HMO and your ESX javascript at best digital marketing companies in India. You might be surprised that it kind of starts to make sense once you do it a couple of times. And overall I'd say it's important to understand inheritance and classes especially in the context of object-oriented coding in Javascript. And for me anyway when I was there and it just took the kind of an adjustment in my thinking. So good luck. Do a lot of practice and you'll be a pro in no time.

bottom of page