Objects in JavaScript By albro

Objects in JavaScript

Let's take a new look at JavaScript; It's use! In other words, in a series of posts, after explaining some prerequisites, such as the types of functions and their calls, and the types of objects, I want to access the DOM. That's where the power of JavaScript is shown in practice and I can use the DOM to change the content of a page and its behavior.

Defining objects in JavaScript

It can be said that objects in JavaScript are everything in JavaScript! If you understand things, many issues will become easy for you and you will gain a lot of maneuvering power.

Almost everything in JavaScript is an object:

  • Booleans can be objects (using new ).
  • Numbers can be objects (using new).
  • Strings can be objects (using new ).
  • Date is always an object.
  • Math is always the object.
  • Regular expressions are always objects.
  • Arrays are always objects.
  • Functions are always objects.
  • And finally, objects are also of object type.

In other words, all JavaScript values (except primitive values) are objects.

primitive values

Primitive values are values that don't have any properties or methods:

  • string
  • number
  • boolean
  • null
  • undefined

Primitive values are immutable in JavaScript. You must be saying that we can easily change the numbers, so how are they immutable?

Let me give you an example; You can change x = 3.14 to x = 2. In this case, you have changed the variable (x) and not 3.14. Another example is that the string "Hello" always remains the same as "Hello" and does not change, but you can give it to a variable or take it from a variable. You cannot change the string itself. JavaScript logic is like this.

Objects are also a variable type, but the difference is that variables can only hold one value, such as var person = "Hive Blockchain", but objects can hold many values.

These values are stored in the form of name: value:

var person = {firstName:"Hive", lastName:"Blockchain", age:3, eyeColor:"red"};

This object can also be written as follows:

var person = {
  firstName : "Hive",
  lastName  : "Blockchain",
  age     : 3,
  eyeColor  : "red"
};

We say that JavaScript objects are a collection of named values.

properties

I said above that values are stored in objects as name: value. We call these names property. Therefore, it can be said for the above object:

Property
Value
firstName
Hive
lastName
Blockchain
age
3
eyeColor
red

Note: The properties of an object can be primitive values, functions or other objects.

methods

Methods in JavaScript are actions performed on an object, but the object method is one of the object's properties that has the definition of a function. Pay attention to the following example:

Property
Value
firstName
Hive
lastName
Blockchain
age
3
eyeColor
red
fullName
function() {return this.firstName + " " + this.lastName;}

So fullName is a method.

Create an object

There are several ways to create an object in JavaScript:

  • Defining an object number manually (object literal)
  • Define an object number using the new keyword
  • Defining an object constructor (meaning "object builder" or "object maker") and then building different objects based on that constructor.

The first method (object literal)

This is the easiest way to create an object. In this method, you put a list of name: values (such as age: 3) inside {} signs. Example:

 var person = {firstName:"Hive", lastName:"Blockchain", age:3, eyeColor:"red"};

As I said, white space does not affect the execution, so you can write this object as follows:

var person = {
  firstName: "Hive",
  lastName: "Blockchain",
  age: 3,
  eyeColor: "red"
};

The second method: using new

An object is defined simply as follows:

var person = new Object();
person.firstName = "Hive";
person.lastName = "Blockchain";
person.age = 3;
person.eyeColor = "red";

This method is the same as the first method. Due to the readability of the code and its faster execution, I suggest you never use this method and choose the first method instead.

A common mistake about objects

If we have an object called person, the following code cannot copy it:

var x = person;

In fact, variable x is not a copy of person, but it is itself, and both are the same. Any changes you make in x will also be made in person. So:

var person = {firstName:"Hive", lastName:"Blockchain", age:3, eyeColor:"red"}
var x = person;
x.age = 4;

This code (i.e. x.age = 10) changes both x and person because both are the same object.



0
0
0.000
5 comments
avatar

Congratulations!


You have obtained a vote from CHESS BROTHERS PROJECT

✅ Good job. Your post has been appreciated and has received support from CHESS BROTHERS ♔ 💪


♟ We invite you to use our hashtag #chessbrothers and learn more about us.

♟♟ You can also reach us on our Discord server and promote your posts there.

♟♟♟ Consider joining our curation trail so we work as a team and you get rewards automatically.

♞♟ Check out our @chessbrotherspro account to learn about the curation process carried out daily by our team.


🏅 If you want to earn profits with your HP delegation and support our project, we invite you to join the Master Investor plan. Here you can learn how to do it.


Kindly

The CHESS BROTHERS team

0
0
0.000
avatar

I haven't learned JavaScript yet but I could still understand all that you have said and that's because objects in JavaScript are a lot similar to dictionaries in python. The same curly brackets {} and property-value pairs (but in python we just call them key-value pairs)

0
0
0.000
avatar

I've always said this and I'll repeat it again: regardless of the different syntax, the principles are always the same. You should always learn the basics.

0
0
0.000
avatar

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. 
 

0
0
0.000