Objects in JavaScript, Python and C

in Programming & Dev3 months ago

Before I got to learn any programming language, I always hear people say if you know one language very well, then it won’t be difficult to learn another, you just have to be familiar with the new syntax but most of the concepts remain the same. I started with C before I moved to python and I learned a bit about python. I realized there are a lot of similarities between both languages and I also read that a lot of python syntax is influenced by C.

Then I started learning JavaScript and it’s basically the same story; there are a lot of similarities between it and the other two languages I learned earlier (C and python). Loops, data types, arrays, and conditional statements are some of the things that are common between these languages. I recently came across JavaScript objects and I will say they are currently my favorite thing in JavaScript, just like how dictionary is my favorite in python but as for structures in C, I wouldn’t say it’s my favorite because of how rigid it is to work with.

JavaScript objects, Python dictionaries, and C structures are very similar in the way they work, but JavaScript and Python provide more flexibility in the way those syntaxes are used and I will explain in a bit but first, let’s talk about how these things look in the 3 languages.

JavaScript Objects

const person = {
name: "John",
dob: 1988,
age: 34,
eyeColor: "blue"
}

The items in an object exist in pairs called properties. In the above code, we declared a variable known as person and the curly braces tell us that this variable isn’t a normal one, but it’s indeed an object. The first item (or property) is name and its value is John. A property and its value are separated by a colon (:), while different properties are separated by a comma.

Python Dictionaries

person = {
"name": "John",
"dob": 1988,
"age": 34,
"eyeColor": "blue"
}

This is basically the same as the JavaScript objects; each item exists in a key-value pair but the only difference here is that each of the keys is enclosed with quotation marks. Aside from that, every other thing is the same.

C structures

typedef struct person {
char name[35];
int dob;
int age;
char eyeColor[10];
} person;

There are a lot of things going on here and that’s usually the problem with C; something that looks very simple to do in other languages is now looking complicated in C, but let's go through the code one by one. The first keyword there typedef is just a way of creating a custom data type in C (in this case, the custom data type is struct person). The next keyword is struct and that’s basically how you specify a structure, unlike other languages where you just have to use only a variable. The last thing at the end of the code is the name of this structure we declared and we called it person.

Within the code, you will notice that I have things like char and int, that’s because we have to specify the data type in C before they can be used, unlike in JavaScript and python where you don’t need to do that. The first variable there is name[35], what’s the 35 doing there? C doesn’t have a default string datatype, so in order to store a string, you have to use an array of characters (That’s one crazy thing about C). What that line simply means is that I am creating a memory space where I can store 35 characters which will represent a string. name[35] doesn’t have any value and that’s because we are not allowed to assign values to each item inside a structure, we do that outside of it.

person newPerson = {"John", 1988, 34, "blue"};

Each of these values is assigned to the variables inside the structure in the way they appear here. So, “John” goes to name[35], 1988 goes to dob, and so on. If you mix it up; maybe you wrote 34 before 1988, 34 will now go to dob while 1988 goes to age. To prevent something like that from happening, we can use another syntax

person newPerson  // creates a new structure called newPerson

newPerson.name = “John” // assigns a value to `name[35]`

Accessing the property values of JavaScript objects,

We can access each property’s value by using two methods; either the dot notation method or the square bracket method.

let name = person["name"];
let dob = person["dob"];
let age = person.age;
let eyeColor = person.eyeColor;

I used both methods here and you can use anyone convenient for you but notice the differences in using both of them. The square bracket method requires you to enclose the property with quotation marks but that isn’t required when using the dot notation method. I assigned each of the values to a variable so that I can easily use it without having to access the value again. Now I can use the variable in a sentence like this:
console.log("The person's name is " + name + ", he is " + age + " years old and the color of his eyes is " + eyeColor);

The output will be: The person’s name is John, he is 34 years old and the color of his eye is blue.

Accessing the key values of python dictionaries

name = person["name"]
dob = person["dob"]
age = person["age"]
eyeColor = person["eyeColor"]

Only the square bracket method can be used to access the values of the keys in a dictionary and it’s a lot similar to how it is done in JavaScript objects
Each value is assigned to a variable and we can use it in a sentence:
print(f"The person's name is {name}, he is {age} years old and the color of his eyes is {eyeColor}")

The output will be: The person’s name is John, he is 34 years old and the color of his eye is blue.

Accessing the item values of C structures

char *name = newPerson.name;
int dob = newPerson.dob;
int age = newPerson.age;
char *eyeColor = newPerson.eyeColor;

The dot notation method is used to access the items of a structure and the values can be stored in a variable to make things easier. *name is a pointer and that’s how we can store string items (string is just an array of characters). name points to the first character of the string (which is J) and we can use that to access every other character in the array.
The variables can now be used in a sentence:
printf("The person's name is %s, he is %i years old and the color of his eyes is %s\n", name, age, eyeColor);

The output will be: The person’s name is John, he is 34 years old and the color of his eye is blue.

Modifying the properties of a JavaScript object

It’s very simple to change the value of a property in an object, we simply have to access the property’s value and give it a new value

person.name = "Peter";
name = person.name

The name property in the person object will now have the value of Peter instead of John. That new property value can now be assigned to the same variable as before (or you can use a new one), then we can use it in the previoue sentence.
The new output will now be: The person’s name is Peter, he is 34 years old and the color of his eye is blue.

Modifying the key values of a python dictionary

It’s very similar to how it is done in JavaScript objects, we just access the value of the key and assign it a new value.

person["name"] = "Peter"
name = person["name"]

When we use it in a sentence, the new output will now be: The person’s name is Peter, he is 34 years old and the color of his eye is blue.

Modifying the items of a C structure

You might be thinking it will be as easy as just doing newPerson.name = “Peter” but this won’t work because C is just not as cooperative as other languages. That might work for other data types like integers and floats; newPerson.age = 40 will work. But to modify the value of a string, we have to use a function called strcpy().
strcpy(newPerson.name, "Peter");

This simply copies Peter and uses it to overwrite whatever is the value of newPerson.name. We don’t need to assign it to a variable before it can be used, the previous variable will still work as usual. So, once we use the printf function to output the new sentence, we will have: The person’s name is Peter, he is 34 years old and the color of his eye is blue.

Adding a new property to an object in JavaScript

If we want to add a new property to the object, maybe something like gender, we can easily do that with:

person.gender = "male";
let gender = person.gender

This will add gender:male to the object and we can now access this property's value just like we did the others and even use it in a sentence:
console.log("The person's name is " + name + ", he is a " + gender + " and he is " + age + " years old. The color of his eyes is " + eyeColor);

This will output: The person’s name is Peter, he is a male and he is 34 years old. The color of his eye is blue.

Adding a new key to a dictionary in Python

It's similar to the syntax used in accessing a key's value. We can use two methods to do this and I will demonstrate them:

person["gender"] = "male"
person.update({"gender": "male"})
gender = person["gender"]

These two syntaxes will do the same thing. The second method uses the update() function to add the new item to the dictionary. We can now access the item's value and assign it to a variable that can now be used in a sentence.

Adding a new item to a structure in C

Unfortunately you can't do this the way it was done in the other languages. Once a structure has been defined, you can't add to it later on. You will have to go back to the main structure body and add whatever new property you want or you can redefine a new structure with a different name, before you can add the new item.

This is why I said earlier that structures in C are rigid, it isn't flexible as objects and dictionaries. If you want to do complicated operations, it's better to use another data structure like hash tables.

Conclusion

JavaScript objects are very powerful and they come with a lot of inbuilt methods (functions) and you can also create your own custom method as one of the properties of the object. I am still learning the various syntaxes and concepts associated with objects because I come from a C language background where we don't have objects and the only thing similar which are structures have a lot of limitations.

Thanks for reading

Connect with me on:
Twitter: @kushyzeena
Readcash: @kushyzee

Lead image: created with Canva
Sort:  


The rewards earned on this comment will go directly to the people( @kushyzee ) sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.

If you know one language very well, then it won’t be difficult to learn another, you just have to be familiar with the new syntax but most of the concepts remain the same

I thought so too, until I learned about functional programming languages. Many ideas and concepts have been ported to non-functional languages these days though, so there is no longer a revolutionary feeling when you learn it.

Exactly, that exciting feeling of learning something new becomes minimal, it's basically just the same concepts or closely similar

Congratulations @kushyzee! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 4250 upvotes.
Your next target is to reach 4500 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

HiveFest⁷ Meetings Contest
Support the HiveBuzz project. Vote for our proposal!

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

I haven't started learning python but I've always understood the concepts of dictionaries, loops and other basic stuff. But as for C, that is really just one complicated language.

You've learnt python too?

It will be difficult to learn C after going through simpler languages like JavaScript and python but knowing it will help to better understand other languages

You've learnt python too?
Yes, but just the basics

Oh that's nice 👍

nice comparisons between different languages!
!1UP

Thank you 😊

1UP-PIZZA.png

You have received a 1UP from @gwajnberg!

The @oneup-cartel will soon upvote you with:
@oneup-curator, @stem-curator, @vyb-curator, @pob-curator
And they will bring !PIZZA 🍕.

Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.

Thank you for sharing this amazing post on HIVE!
  • Your content got selected by our fellow curator @priyanarc & you just received a little thank you via an upvote from our non-profit curation initiative!

  • You will be featured in one of our recurring curation compilations and on our pinterest boards! Both are aiming to offer you a stage to widen your audience within and outside of the DIY scene of hive.

Join the official DIYHub community on HIVE and show us more of your amazing work and feel free to connect with us and other DIYers via our discord server: https://discord.gg/mY5uCfQ !

If you want to support our goal to motivate other DIY/art/music/homesteading/... creators just delegate to us and earn 100% of your curation rewards!

Stay creative & hive on!

I really love to know about JavaScript, python and c. I enjoyed reading this and I really wish I could have an opportunity to learn it practically.

You're welcome to join the world of programming if it's something you like 😁

Will you teach me? 🙃

You can use the same website I am using to learn: freecodecamp.com

Wow!! Thank you so much.. 🙏🙏🙏