Name an actual real world example of why i would need to use these:

After almost completing my first JavaScript lesson, one thing has come to my attention:

  • A few of the techniques that are being taught in these lessons are overcomplicated solutions to very simple problems.

Example1:

Q: Check a variable to see if it contains a property using this over complicated equation.

A: Wouldnt I just be able to look and see if its there? Like, what purpose does this over complicated equation serve?

Example2:

Q: Use this over complicated equation to add a property to a variable:

A: Why would I do that instead of just going straight to the variable and adding it in that way?

Like what is the point and real world use for these??

No. The point of code is so that you do not need manual intervention. Handling objects in different states with the same logic is so common that syntax to support this has actually been expanded.

Are you talking about getters and setters? Protecting and escapsulating interior logic of an object is very common. This lets you independently update the design or internal code of an object without having to change all of the code that uses that object.

1 Like

There was a lesson i learned where you us like “add” and “delete” where you can add and delete properties from a variable. IMO, it seems like that just adds more clutter to your code, whereas, you can just go straight to the variable and add/ delete properties manually.

EX:
const myDogs {
dog1: “Jake”;
dog2: “Spike”;
dog3: “Spot”;
}

If I were to add another property (dog4:“Bark”), the lesson told me I had to use this equation where I put:

add myDogs(dog4[“Bark”])

Instead of doing the above, why cant I just do this?:

const myDogs {
dog1: “Jake”;
dog2: “Spike”;
dog3: “Spot”;
dog4: “Bark”;
}

Objects need to be able to change as the code runs.

As a small example, think of a video game. We need a way to dynamically change the contents of the player’s inventory as they move through the game, picking up and consuming items.

1 Like

ahhh okay that makes more sense. Is JavaScript used in video games? or is that more C++?

There are some games written in JavaScript, but the point is that this sort of use case comes up all the time.

1 Like

awesome fam, thanks for the help today :blush:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.